2 Hexdecimal to decimal

Hi,

How can I combine 2 hex decimal to become a decimal number?

E.g. 0x04 and 0xF0 = 1264 (decimal)
0x12 and 0xE0 = 4832 (decimal)

Thanks

Multiply the first by 256 and add the second.

E.g. 0x04 and 0xF0 → 0x04 * 256 + 0xF0 = 0x04F0 = 1264 (decimal)
0x12 and 0xE0 → 0x12 * 256 + 0xE0 = 0x12E0 = 4832 (decimal)

Instead of multiply by 256 you can shift left by 8. Same thing.

E.g. 0x04 and 0xF0 → 0x04 << 8 + 0xF0 = 0x04F0 = 1264 (decimal)
0x12 and 0xE0 → 0x12 << 8 + 0xE0 = 0x12E0 = 4832 (decimal)

Instead of adding you can use a bitwise OR. Same effect.

E.g. 0x04 and 0xF0 → 0x04 << 8 | 0xF0 = 0x04F0 = 1264 (decimal)
0x12 and 0xE0 → 0x12 << 8 | 0xE0 = 0x12E0 = 4832 (decimal)

Hi,
I do a simple trial but I could’nt get the correct answer. Can you help?

byte one = 0x04;
byte two = 0xF0;

void setup(){
Serial.begin(9600);
Serial.println(one, HEX);
byte three = one << 8;
Serial.println(two, HEX);
Serial.println(three,HEX );
three = three + two;
Serial.println(three, HEX);
Serial.println(three);
}

void loop(){}

Serial monitor:

4
F0
0
F0
240

"three" needs to be an int, not a byte

byte one = 0x04;
byte two = 0xF0;

A single heximal digit is just one digit. Four bits.

In your byte "two", you have already shifted the digit F to the high position in the byte. You don't want to shift it again.

How can I combine 2 hex decimal to become a decimal number?

E.g. 0x04 and 0xF0 = 1264 (decimal)

If you want to form the hexadecimal number 0x04F0 , which according to your result 1264, you
apparently want to do, then you have four hexadecimal digits not 2.

Thanks all, I have got it.