How to `Serial.print()` “full” hexadecimal bytes?

橙三吉。 提交于 2020-12-26 06:32:16

问题


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:


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.



回答2:


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




回答3:


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




回答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);



回答5:


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));



回答6:


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.



来源:https://stackoverflow.com/questions/19127945/how-to-serial-print-full-hexadecimal-bytes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!