I have an array of byte :
uint8_t* data = 10101010 01000001 00000000 00010010 00000000 00000010..........
uint8_t get_U8(uint8_t * data, int* offset)
{
uint8_t tmp = *((uint8_t*)(data + *offset));
*offset += sizeof(uint8_t);
return tmp;
}
uint16_t get_U16(uint8_t* data, int* offset)
{
uint16_t tmp = *((uint16_t*)(data + *offset));
*offset += sizeof(uint16_t);
return tmp;
}
offset here is 2.
get_U8(data, 0) = 10101010 = 170 ===> OK
get_U8(data, 1) = 01000001 = 65 ===> OK
get_U8(data, 2) = 00000000 = 0 ===> OK
get_U8(data, 3) = 00010010 = 18 ===> OK
but
get_U16(data, 2) = 4608 ===> NOT OK (should be 18)
4608 = 00010010 00000000
So I understand that the 2 bytes are inverted.
I don't understand why get_U16
is inverting the position of the bytes, and it's not a big endian / little endian issue because here it's the first 8 bits inverted with the 8 second bits.
I am just expecting uint16_t
to just take the 16 bits at the given position, and return 18 here.
Can anyone tell me what I am doing wrong?