hello i have an array of 3 bytes unsgined char and i want to convert the 3 byte (hex) array to 1 byte (hex value). values in the array are between 0 - 9 only.
unsigned char buf[3], output; buf[0] = 0x01; buf[0] = 0x02; buf[0] = 0x03;
i want an output as follow: 123 in HEX => output = 0x7B
i tried 'sscanf' and i haven't succeeded. please help.
sounds pretty simple to me: all you have to do is multiply your most significant byte by 100 add to it the next byte multiplied by 10, and then add the least significant byte. then all you have to do printf or sprintf your data.
thanks very much, easy.
printf("\n%bx", buf[0]*100 + buf[1]*10 + buf[2]);
Of course, the whole thing will produce unexpected results if the resulting number would be greater than 255.
123 in HEX => output = 0x7B
That makes no sense at all. There's absolutely nothing "HEX" about a "123". That's decimal or text, depending on whether by '2' you mean a byte valued exactly 2, or a byte with the character code for '2'.
From his example soruce, you can see that he means the numeric value 2, not the ASCII character 2.
Byt three bytes with the range 0..9 in them can still form any value up to 999 which is almost four times the capacity of a single byte.
http://www.keil.com/support/man/docs/c51/c51_atoi.htm