You would piece together the bytes into an integer yourself.
If you are on a big endian host and your current code did get the proper integer value, use this one:
/** De-serialize a uint64_t from a byte array in big endian format.
* @param r The byte array to containing the integer in. Must be at least of size 4.
* @param return The deserialized integer from the byte array
*/
static inline uint64_t uc_unpack_64_be(const uint8_t *r)
{
uint64_t v;
v = (uint64_t)r[0] << 56;
v |= (uint64_t)r[1] << 48;
v |= (uint64_t)r[2] << 40;
v |= (uint64_t)r[3] << 32;
v |= (uint32_t)r[4] << 24;
v |= r[5] << 16;
v |= r[6] << 8;
v |= r[7];
return v;
}
If you are currently on a little endian machine, use this one:
/** De-serialize a uint64_t from a byte array in little endian format.
* @param r The byte array to containing the integer in. Must be at least of size 8.
* @param return The deserialized integer from the byte array
*/
static inline uint64_t uc_unpack_64_le(const uint8_t *r)
{
uint64_t v;
v = r[0];
v |= r[1] << 8;
v |= r[2] << 16;
v |= (uint32_t)r[3] << 24;
v |= (uint64_t)r[4] << 32;
v |= (uint64_t)r[5] << 40;
v |= (uint64_t)r[6] << 48;
v |= (uint64_t)r[7] << 56;
return v;
}
Use it e.g. as uint64_t myqword = uc_unpack_64_le(&buffer[16]);
Note that whether you use one of uint64_t uc_unpack_64_le
or uint64_t uc_unpack_64_le
functions depends on whether you formatted the data in your buffer
as little or big endian, not if your code currently runs on a little or big endian machine.
If you insist on using your current long long and char types, change the code accordingly, but I encourage you to use the uint16_t and uint64_t types from the <stdint.h>
header instead.