C++ 如何`Serial.print()`“完整”十六进制字节?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19127945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to `Serial.print()` "full" hexadecimal bytes?
提问by Backo
I am programming Arduinoand I am trying to Serial.print()
bytes in hexadecimal format "the my way" (keep reading for more information).
我正在对Arduino 进行编程,并且我正在尝试以Serial.print()
“我的方式”以十六进制格式进行字节处理(请继续阅读以获取更多信息)。
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 0
s (05
instead of 0
and 00
instead of 0
).
换句话说,我想打印“完整”的十六进制值,包括0
s (05
而不是0
和00
而不是0
)。
How can I make that?
我怎样才能做到这一点?
回答by HymanCColeman
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.
回答by G1l0uX
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(byteX) { if (X < 10) {Serial.print("0");} ...
void p( byteX) { if (X < 1 0) {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
希望这可以帮助
回答by thoredge
Use sprintf to print into a buffer (two chars per byte + null terminator):
使用 sprintf 打印到缓冲区(每字节两个字符 + 空终止符):
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.
在中间添加新行以使每个行都在新行上。关于“%02x”,% 表示这里有格式信息,0 表示用 0 填充,2 表示填充输入直到 2 个字符宽,x 表示以十六进制形式给我。
For other formatting options see http://linux.die.net/man/3/sprintf
有关其他格式选项,请参阅http://linux.die.net/man/3/sprintf
回答by Keroronsk
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));