Skip to content

Commit 0b0521a

Browse files
committed
Dramatically simplify the "littleEndianToInt" function. Previous version was very silly.
1 parent 7271d51 commit 0b0521a

File tree

1 file changed

+3
-18
lines changed

1 file changed

+3
-18
lines changed

vdi.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -171,24 +171,9 @@ uint64_t vdi::littleEndianToInt(const char *buffer, uint8_t size) {
171171
throw std::invalid_argument("'size' should never be greater than 8");
172172
}
173173

174-
// save existing stringstream settings (to restore later)
175-
std::ios_base::fmtflags oldFlags(std::stringstream().flags());
176-
177-
// stringstream to store buffer in reverse order
178-
std::stringstream ss;
179-
180-
// loop through buffer in reverse
181-
for (int8_t i = size - 1; i != -1; --i) {
182-
// concatenate each character in buffer as hex to the stringstream
183-
ss << std::hex << std::setw(2) << std::setfill('0') << (int)(uint8_t)buffer[i];
184-
}
185-
186-
// store resulting stringstream as int
187-
int result;
188-
ss >> result;
189-
190-
// restore stringstream settings
191-
std::stringstream().flags(oldFlags);
174+
// copy the buffer memory into a 64-bit int
175+
uint64_t result;
176+
memcpy(&result, buffer, size);
192177

193178
return result;
194179
}

0 commit comments

Comments
 (0)