简体   繁体   English

为什么 printf 在打印十六进制时不只打印出一个字节?

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

pixel_data is a vector of char . pixel_datacharvector

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的功能?

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 varargs function.您可能会遇到未定义行为的良性形式,因为%x修饰符需要一个unsigned int参数,而当传递给varargs函数时, char通常会被提升为int

You should explicitly cast the char to an unsigned int to get predictable results:您应该将 char 显式转换为unsigned int以获得可预测的结果:

printf(" 0x%1x ", (unsigned)pixel_data[0] );

Note that a field width of one is not very useful.请注意,字段宽度为 1 不是很有用。 It merely specifies the minimum number of digits to display and at least one digit will be needed in any case.它仅指定要显示的最少位数,并且在任何情况下都至少需要一位。

If char on your platform is signed then this conversion will convert negative char values to large unsigned int values (eg fffffff5 ).如果您平台上的char是有符号的,那么此转换会将负char值转换为大的unsigned int值(例如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.如果要将字节值视为无符号值,并且在转换为unsigned int时仅将零扩展为unsigned int ,则应将unsigned char用于pixel_data ,或通过unsigned char或在升级后使用屏蔽操作。

eg例如

printf(" 0x%x ", (unsigned)(unsigned char)pixel_data[0] );

or或者

printf(" 0x%x ", (unsigned)pixel_data[0] & 0xffU );

Better use the standard-format-flags更好地使用标准格式标志

printf(" %#1x ", pixel_data[0] );

then your compiler puts the hex-prefix for you.然后您的编译器为您放置十六进制前缀。

使用%hhx

printf("%#04hhx ", foo);

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

Width-specifier in printf is actually min-width. printf宽度说明符实际上是最小宽度。 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(" 0x%2x ", pixel_data[0] & 0xff)来打印低字节(注意 2,如果pixel_data[0]是例如0xffffff02 ,则实际打印两个字符)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不会只打印一个字母就打印所有字母,但需要在输入的字母后打印每个字母 - not printing out all alphabet just one letter but it needs to print out every letter after the one that was entered 为什么这个嵌套的 printf 语句打印“5 53”? - Why does this nested printf statement print "5 53"? printf()混淆-尝试打印字符串时打印(空),而在打印缓冲区时打印正确的值 - printf() confusion - printing (null) when trying to print string and printing correct value when printing buffer 为什么打印出来的元组中只有一个可以工作,但是打印出来就可以了? - Why does only one of my return tuple work but is fine when I print it out? 为什么std :: cout不打印并集值,而printf可以呢? - Why does std::cout not print values from union, but printf does? 为什么C ++在使用std :: hex而不是资本时会打印小写字母? - Why does C++ print small case alphabet when using std::hex and not capital? 为什么这个 C++ 代码不应该打印出变量? - Why does this C++ code print out variables when it is not supposed to? 打印出浮子的内六角 - Printing out the Internal Hex of a float 在JavaScript中打印字节数组的十六进制或十进制值 - Print hex or decimal values of byte array in JavaScript 为什么这个 C++ 代码打印出长度为 5 并且当我打印出字符串时程序会自动终止? - Why does this c++ code print out length 5 and when i'm print out string the program is automatic terminate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM