I have a 16 bit variable and I want to read the high byte as an 8-bit value. I tried right shifting by 8 bits and casting the value as a uint8_t type. But I do not get the correct value, at least I don’t get what I was expecting.
Here is some code:
uint16_t dataLength, dataLength2; Serial.begin(9600); dataLength = 0x017F; Serial.println(dataLength, HEX); dataLength2 = dataLength >> 8; Serial.println (((dataLength2),HEX)); Serial.println((uint8_t) dataLength, HEX);
I expected to get 0x01 as the high byte value, but I am getting 0x16!!! What am I doing wrong with this seemingly simple operation?!?