How to `Serial.print()` "full" hexadecimal bytes?
Asked Answered
E

6

10

I am programming Arduino and I am trying to Serial.print() bytes in hexadecimal format "the my way" (keep reading for more information).

That is, by using the following code

byte byte1 = 0xA2;
byte byte2 = 0x05;
byte byte3 = 0x00;

Serial.println(byte1, HEX);
Serial.println(byte2, HEX);
Serial.println(byte3, HEX);

I get the following output in the Serial Monitor:

A2
5
0

However I would like to output the following:

A2
05
00

In words, I would like to print the "full" hexadecimal value including 0s (05 instead of 0 and 00 instead of 0).

How can I make that?

Eaglewood answered 1/10, 2013 at 23:59 Comment(9)
Can you print them to a string in the proper format and then send that string via the serial port?Alexandriaalexandrian
@Retired Ninja - What do you mean with "print them to a string in the proper format"? How can I make that?Eaglewood
sprintf(buffer, "%02x", number); Serial.println(buffer);Alexandriaalexandrian
I doubt Arduino supports sprintf.Glottic
I'm not familiar with what it supports and how, but a quick google leads me to believe sprintf is available but not for floating point numbers without some hassle. There's also this that may work for you: playground.arduino.cc/Main/PrintfAlexandriaalexandrian
sprintf seems to be supported by Arduino.Eaglewood
and what if you want to print 0xFA2, you will need to also print 005 and 000?Dampen
@Dampen - The 0xFA2 byte exists?Eaglewood
nope, you'll need an int16_t. I was asking in case you need to change the type of what you are printing to think of a robust answer.Dampen
A
8

Simple brute force method, is to write a routine as:

void p(char X) {

   if (X < 16) {Serial.print("0");}

   Serial.println(X, HEX);

}

And in the main code:

p(byte1);  // etc.
Alyworth answered 2/10, 2013 at 2:6 Comment(2)
With the sprintf function (as @Retired Ninja said in a previous comment) it is more simple... why should I use your code?Eaglewood
@Backo, load module size. That is, how much code does sprintf generate for your embedded system? If your sketch gets too large you will have to give-up some luxuries. But, for a small sketch whatever works is obviously acceptable.Alyworth
D
5

The lowest footprint in Memory, Code and runtime would be classic bit playing

byte b;
Serial.print(b>>4,  HEX);
Serial.print(b&0x0F,HEX);

Which is working fine on any 8bit type. For any other mask also the first line to

Serial.print((b>>4)&0x0F,  HEX);
Diagraph answered 19/6, 2020 at 8:30 Comment(0)
P
3

sorry - not enough reputation to comment but found previous answer is not fully correct. Actually, the nice light way to code it should be :

void p(byte X) { if (X < 10) {Serial.print("0");} ...

giving the code:

void p(byte X) {

   if (X < 10) {Serial.print("0");}

   Serial.println(X, HEX);

}

And in the main code:

p(byte1);  // etc.

hope this helps

Pumpkin answered 18/8, 2015 at 21:27 Comment(2)
X < 10 ? 16 maybe, but not 10;Judkins
Agreed, just modify it to test for < 0x10 and you're done :-)Funnelform
H
2

Use sprintf to print into a buffer (two chars per byte + null terminator):

byte byte1 = 0xA2;
byte byte2 = 0x05;
byte byte3 = 0x00;
char s[7];
sprintf(s, "%02x\n%02x\n%02x", byte1, byte2, byte3);
Serial.println(s);

Added new lines in between to get each on new line. About '%02x', the % means here comes formatting information, 0 means to pad with 0, 2 means pad input until 2 characters wide and x means give me this as hexadecimal.

For other formatting options see http://linux.die.net/man/3/sprintf

Howdy answered 19/8, 2016 at 13:33 Comment(0)
D
0

Try this:

//Converts the upper nibble of a binary value to a hexadecimal ASCII byte.
//For example, btohexa_high(0xAE) will return 'A'.
unsigned char btohexa_high(unsigned char b)
{
    b >>= 4;
    return (b>0x9u) ? b+'A'-10:b+'0';
}


//Converts the lower nibble of a binary value to a hexadecimal ASCII byte.
//  For example, btohexa_low(0xAE) will return 'E'.


unsigned char btohexa_low(unsigned char b)
{
    b &= 0x0F;
    return (b>9u) ? b+'A'-10:b+'0';
}

And in main code:

comand_mod=0xA1; //example variable
Serial.print(btohexa_high(comand_mod));
Serial.print(btohexa_low(comand_mod));
Darra answered 2/10, 2013 at 3:20 Comment(0)
E
0

wow! 7 years ago and I felt here, my answer might be useful for you (hopefully not anymore) or others looking for the answers like me.

Use "Serial.write()" to send a hex byte over serial.

All Serial.print() eg. println, printf, sprint, print will "print" your value in ASCII.

Engaged answered 20/10, 2020 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.