C++ 为什么 printf 在打印十六进制时不只打印出一个字节?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3555791/
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
Why does printf not print out just one byte when printing hex?
提问by BeeBand
pixel_data
is a vector
of char
.
pixel_data
是vector
的char
。
When I do printf(" 0x%1x ", pixel_data[0] )
I'm expecting to see 0xf5
.
当我这样做时,printf(" 0x%1x ", pixel_data[0] )
我期待看到0xf5
.
But I get 0xfffffff5
as though I was printing out a 4 byte integer instead of 1 byte.
但是我0xfffffff5
好像打印了一个 4 字节的整数而不是 1 字节。
Why is this? I have given printf
a char
to print out - it's only 1 byte, so why is printf
printing 4?
为什么是这样?我已经给出printf
了一个char
打印出来 - 它只有 1 个字节,那么为什么要printf
打印 4 个呢?
NB. the printf
implementation is wrapped up inside a third party API but just wondering if this is a feature of standard printf
?
注意。该printf
实现包含在第三方 API 中,但只是想知道这是否是标准功能printf
?
回答by CB Bailey
You're probably getting a benign form of undefined behaviour because the %x
modifier expects an unsigned int
parameter and a char
will usually be promoted to an int
when passed to a varargsfunction.
您可能会遇到未定义行为的良性形式,因为%x
修饰符需要一个unsigned int
参数,并且 achar
通常会int
在传递给varargs函数时提升为 an 。
You should explicitly cast the char to an unsigned int
to get predictable results:
您应该将 char 显式转换为 anunsigned int
以获得可预测的结果:
printf(" 0x%1x ", (unsigned)pixel_data[0] );
Note that a field widthof one is not very useful. It merely specifies the minimum number of digits to display and at least one digit will be needed in any case.
请注意,字段宽度为 1 不是很有用。它仅指定要显示的最少位数,并且在任何情况下都至少需要一位。
If char
on your platform is signed then this conversion will convert negative char
values to large unsigned int
values (e.g. fffffff5
). If you want to treat byte values as unsigned values and just zero extend when converting to unsigned int
you should use unsigned char
for pixel_data
, or cast via unsigned char
or use a masking operation after promotion.
如果char
在您的平台上已签名,则此转换会将负值转换char
为大unsigned int
值(例如fffffff5
)。如果您想将字节值视为无符号值,并且在转换为时只扩展零,unsigned int
则应使用unsigned char
for pixel_data
,或unsigned char
在提升后通过转换或使用掩码操作。
e.g.
例如
printf(" 0x%x ", (unsigned)(unsigned char)pixel_data[0] );
or
或者
printf(" 0x%x ", (unsigned)pixel_data[0] & 0xffU );
回答by user411313
Better use the standard-format-flags
更好地使用标准格式标志
printf(" %#1x ", pixel_data[0] );
then your compiler puts the hex-prefix for you.
然后您的编译器为您放置十六进制前缀。
回答by domen
Use %hhx
用 %hhx
printf("%#04hhx ", foo);
回答by leppie
Then length
modifier is the minimum length.
那么length
修饰符是最小长度。
回答by phadej
Width-specifier in printf
is actually min-width. You can do printf(" 0x%2x ", pixel_data[0] & 0xff)
to print lowes byte (notice 2, to actually print two characters if pixel_data[0]
is eg 0xffffff02
).
宽度说明符printf
实际上是最小宽度。您可以printf(" 0x%2x ", pixel_data[0] & 0xff)
打印低字节(注意 2,如果pixel_data[0]
是,则实际打印两个字符,例如0xffffff02
)。