You should not use built-in types for serialization; instead, when you need to know the exact type size, you need fixed-width types:
#include <stdint.h> unsigned char buf[8]; // 64-bit raw data uint64_t little_endian_value = (uint64_t)buf[0] + ((uint64_t)buf[1] << 8) + ((uint64_t)buf[2] << 16) + ... + ((uint64_t)buf[7] << 56); uint64_t big_endian_value = (uint64_t)buf[7] + ((uint64_t)buf[6] << 8) + ((uint64_t)buf[5] << 16) + ... + ((uint64_t)buf[0] << 56);
Similarly, for 32-bit values, use uint32_t . Make sure the source buffer uses unsigned characters.
source share