minor refact
This commit is contained in:
+10
-8
@@ -15,21 +15,23 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool is_big_endian(void){
|
bool is_big_endian(void){
|
||||||
union {
|
uint32_t ui32_v = 0x01020304;
|
||||||
uint32_t i;
|
char bytes[sizeof(uint32_t)];
|
||||||
char c[4];
|
std::memcpy(bytes, &ui32_v, sizeof(uint32_t));
|
||||||
} bint = {0x01020304};
|
|
||||||
|
|
||||||
return bint.c[0] == 1;
|
return bytes[0] == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::int32_t convert_to_int(char* buffer, std::size_t len){
|
std::int32_t convert_to_int(char* buffer, std::size_t len){
|
||||||
std::int32_t a = 0;
|
std::int32_t a = 0;
|
||||||
if(!is_big_endian())
|
if (!is_big_endian()) {
|
||||||
std::memcpy(&a, buffer, len);
|
std::memcpy(&a, buffer, len);
|
||||||
else
|
}
|
||||||
for(std::size_t i = 0; i < len; ++i)
|
else {
|
||||||
|
for (std::size_t i = 0; i < len; ++i) {
|
||||||
reinterpret_cast<char*>(&a)[3 - i] = buffer[i];
|
reinterpret_cast<char*>(&a)[3 - i] = buffer[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user