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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:07:00  来源:igfitidea点击:

Why does printf not print out just one byte when printing hex?

c++cprintf

提问by BeeBand

pixel_datais a vectorof char.

pixel_datavectorchar

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 0xfffffff5as though I was printing out a 4 byte integer instead of 1 byte.

但是我0xfffffff5好像打印了一个 4 字节的整数而不是 1 字节。

Why is this? I have given printfa charto print out - it's only 1 byte, so why is printfprinting 4?

为什么是这样?我已经给出printf了一个char打印出来 - 它只有 1 个字节,那么为什么要printf打印 4 个呢?

NB. the printfimplementation 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 %xmodifier expects an unsigned intparameter and a charwill usually be promoted to an intwhen passed to a varargsfunction.

您可能会遇到未定义行为的良性形式,因为%x修饰符需要一个unsigned int参数,并且 achar通常会int在传递给varargs函数时提升为 an 。

You should explicitly cast the char to an unsigned intto 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 charon your platform is signed then this conversion will convert negative charvalues to large unsigned intvalues (e.g. fffffff5). If you want to treat byte values as unsigned values and just zero extend when converting to unsigned intyou should use unsigned charfor pixel_data, or cast via unsigned charor use a masking operation after promotion.

如果char在您的平台上已签名,则此转换会将负值转换char为大unsigned int值(例如fffffff5)。如果您想将字节值视为无符号值,并且在转换为时只扩展零,unsigned int则应使用unsigned charfor 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 lengthmodifier is the minimum length.

那么length修饰符是最小长度。

回答by phadej

Width-specifier in printfis 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)。