# split a received byte into separate digits and convert to ASCII

Hi everyone...had a quick question regarding converting some received data into ASCII.

Ive googled this for the past couple hours and there are tons of "converting between ASCII/hex/decimal" threads, but none seem to quite match what Im trying to do as far as splitting up the two characters that make up one byte in hex.

I have to perform several steps here.

1. Convert a 0-255 decimal byte value into a 0x00-0xFF hex character.

2. Convert the two letter hex character into two separate ASCII values, representing the number/letter of each hex character.

For example:

starting with: byte myByte = 156 decimal

Process:

decimal 156 = 0x9C hex

Convert 0x9C into two separate ASCII characters "9" and "C"

Convert ASCII 9 and ASCII C back into either decimal or hex values.

end result:

myConvertedByte1 = 57 (hex 0x39, ASCII 9)
myConvertedByte2 = 67 (hex 0x43, ASCII C)

Maybe bitRead the 4 most significant bits, then the 4 least significant bits.....then if its decimal 10, 11, 12, 13, 14, 15...add 55...and that will get ASCII A, B, C, D, E, F. If the 4 bits decimal value is less than 10....add 48....and that will get ASCII 1, 2, 3, 4, 5, 6, 7, 8?

Seems awfully clunky and complicated though...there has to be a nice simple way??

(what im actually trying to do is read in 2 bytes of a CAN-bus message, and then convert those 2 bytes into 4 separate ASCII characters).

This is outputted ASCII is NOT for use with displaying data to the arduino serial terminal or an LCD, so I cant just use "Serial.println(myByte, HEX)" or something.

Thanks

Maybe itoa?

``````void setup()
{

Serial.begin(57600);
byte x = 0xF5;
byte y = 0x10;
char buffer;
itoa(x, buffer, 16);
itoa(y, &buffer, 16);
Serial.println(buffer);
}

void loop()
{

}
``````

You've got 1 and 2 confused. When it is a number it is just a number and it is only ever stored as binary. If you have anything that looks like 0x00 or 0xFF then it would already have to be ascii. You just need itoa(). Hexadecimal is a representation of a number. It's a way of writing a number. But it doesn't change the number itself.

decimal 156 = 0x9C hex

Realize that in this step nothing changed.