How to print hex starts with 0x0x in 2 digits.

Hello everyone,

I am very new to Arduino and i am trying to figure out how to convert hex numbers to strings.

Actually i have successfully managed it but i am having a problem with the hex number starting with "0"

When i convert a number for example (0x06) It prints it as (6) only. Is there a way to make it print as (06)

I have tried Serial.print(0x06, HEX) and

byte d=0x06;
itoa (d, buffer, 10);
Serial.print(d);

They all print it as (6)

I need this because i have 19 bytes data stream from IMU and when i add up the stream it becomes at least 1 byte short. It seems it's dropping out bytes but when i print it as in (byte) instead of converting to (HEX) in HEX view i can see the data in 19 bytes. I need to convert this to human readable text so it will make sense for data analyzing

Thank you.

if (x < 16)

Might be a starting point

there are optional formatter that can be used to coerce output format.

sprintf() can be used too - there are a lot of C functions that can be used see - avr-libc: AVR Libc -

byte d=0x06;
   itoa (d, buffer, 10);
Serial.print(d);

The third argument to the itoa() function is the base. If you want buffer to contain a hex representation of a number, why are you specifying base 10?

Thank you everyone.

Actually Sprintf worked with the right configuration.

I used the following code.

char buffer[3];
byte d=0x0a;
sprintf (buffer, "%02x", d);

And it works. Thank you.