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

发布于 2024-09-15 14:14:14 字数 387 浏览 3 评论 0原文

pixel_datachar向量

当我执行 printf(" 0x%1x ", Pixel_data[0] ) 时,我期望看到 0xf5

但我得到 0xfffffff5 ,就好像我打印的是 4 字节整数而不是 1 字节。

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

注意。 printf 实现包含在第三方 API 中,但只是想知道这是否是标准 printf 的功能?

pixel_data is a vector of char.

When I do printf(" 0x%1x ", pixel_data[0] ) I'm expecting to see 0xf5.

But I get 0xfffffff5 as though I was printing out a 4 byte integer instead of 1 byte.

Why is this? I have given printf a char to print out - it's only 1 byte, so why is printf printing 4?

NB. the printf implementation is wrapped up inside a third party API but just wondering if this is a feature of standard printf?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

奢华的一滴泪 2024-09-22 14:14:14

您可能会得到一种良性形式的未定义行为,因为 %x 修饰符需要一个 unsigned int 参数,而 char 通常会被提升为传递给 varargs 函数时为 int

您应该将 char 显式转换为 unsigned int 以获得可预测的结果:

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

请注意,字段宽度为 1 并不是很有用。它仅指定要显示的最小位数,并且在任何情况下都至少需要一位数字。

如果您平台上的 char 已签名,则此转换会将负 char 值转换为大的 unsigned int 值(例如 fffffff5)。如果您想将字节值视为无符号值,并且在转换为 unsigned int 时仅进行零扩展,则应使用 unsigned char 来表示 pixel_data,或强制转换通过 unsigned char 或在升级后使用掩码操作。

例如

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

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

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.

You should explicitly cast the char to an unsigned int to get predictable results:

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

Note that a field width of 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.

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.

e.g.

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

or

printf(" 0x%x ", (unsigned)pixel_data[0] & 0xffU );
可爱暴击 2024-09-22 14:14:14

使用%hhx

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

Use %hhx

printf("%#04hhx ", foo);
梦魇绽荼蘼 2024-09-22 14:14:14

最好使用标准格式标志,

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

然后编译器为您添加十六进制前缀。

Better use the standard-format-flags

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

then your compiler puts the hex-prefix for you.

甜宝宝 2024-09-22 14:14:14

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

Then length modifier is the minimum length.

绿萝 2024-09-22 14:14:14

printf 中的宽度说明符实际上是最小宽度。您可以执行 printf(" 0x%2x ", Pixel_data[0] & 0xff) 来打印低字节(注意 2,如果 pixel_data[0] 则实际打印两个字符> 例如0xffffff02)。

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

晨与橙与城 2024-09-22 14:14:14

如果没有 0x 前缀,十六进制值可能看起来很奇怪,因此我还使用大写 X 来打印 0xEA 而不是 0Xea:

printf ( "The byte is 0x%hhX", thebyte );

Without 0x prefix, an hex value may look strange, so I use also with capital X to print like 0xEA instead of 0Xea:

printf ( "The byte is 0x%hhX", thebyte );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文