I want to separate a hexadecimal value into 3 bytes but the code is outputting
Original Hex Value: 0x5EEF
Decimal Values: {0, 94, 239}
The byte I'm using to test is 0xbb5eef
I don't see a flaw in the code and I am wondering why only the first byte is incorrect.
void setup() {
// Original hexadecimal value
unsigned int hexValue = 0xbb5eef;
// Extract individual bytes
byte byte1 = (hexValue >> 16) & 0xFF;
//187
byte byte2 = (hexValue >> 8) & 0xFF;
//94
byte byte3 = hexValue & 0xFF;
//239
int decimalValues[] = {byte1, byte2, byte3};
// Print the original value and the array of decimal values
Serial.begin(9600);
Serial.print("Original Hex Value: 0x");
Serial.println(hexValue, HEX);
Serial.print("Decimal Values: {");
for (int i = 0; i < 3; ++i) {
Serial.print(decimalValues[i]);
if (i < 2) {
Serial.print(", ");
}
}
Serial.println("}");
}
void loop() {
// pass
}
I would imagine that the size of an unsigned int is only 16 bits on whatever board you're trying this with. Crank up your warning level and you'll get a 'large integer implicitly truncated to unsigned type' warning.