In a C application (not C++) I have a byte array of data received over the network. The array is 9 bytes long. The bytes 1 to 9 represent a 64-bit integer value as little endian. My CPU also uses little endian.
How can I convert these bytes from the array to an integer number?
I’ve tried this:
uint8_t rx_buffer[2000];
//recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, ...)
int64_t sender_time_us = *(rx_buffer + 1);
But it gives me values like 89, 219, 234 or 27. The sender sees the values as 1647719702937548, 1647719733002117 or 1647719743790424. (These examples don’t match, they’re just random samples.)
>Solution :
Tou need to cast your pointer, like so:
int64_t sender_time_us = *(int64_t*)(rx_buffer + 1);
As it is, you’re only getting one byte of data.