9

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?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Backo
  • 18,291
  • 27
  • 103
  • 170

6 Answers6

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.
JackCColeman
  • 3,777
  • 1
  • 15
  • 21
  • With the `sprintf` function (as @Retired Ninja said in a previous comment) it is more simple... why should I use your code? – Backo Oct 02 '13 at 10:07
  • 3
    @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. – JackCColeman Oct 03 '13 at 04:45
4

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);
Patrick Z
  • 194
  • 9
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

G1l0uX
  • 39
  • 2
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

thoredge
  • 12,237
  • 1
  • 40
  • 55
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));
Keroronsk
  • 120
  • 1
  • 5
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.

Marcelo
  • 65
  • 2
  • 10