printf()格式为十六进制 [英] printf() formatting for hexadecimal

查看:90
本文介绍了printf()格式为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么当打印十六进制数字作为带有前导零的8位数字时,%#08X not 显示的结果与 0x%08X相同?

Why, when printing a number in hexadecimal as an 8 digit number with leading zeros, does %#08X not display the same result as 0x%08X?

当我尝试使用前者时, 08 格式设置标记将被删除,并且仅对 8 无效.

When I try to use the former, the 08 formatting flag is removed, and it doesn't work with just 8.

推荐答案

#部分在输出字符串中为您提供了 0x . 0 x 相对于 08 部分中列出的"8"个字符.如果要保持一致,则需要输入10个字符.

The # part gives you a 0x in the output string. The 0 and the x count against your "8" characters listed in the 08 part. You need to ask for 10 characters if you want it to be the same.

int i = 7;

printf("%#010x\n", i);  // gives 0x00000007
printf("0x%08x\n", i);  // gives 0x00000007
printf("%#08x\n", i);   // gives 0x000007

还更改 x 的大小写会影响输出字符的大小写.

Also changing the case of x, affects the casing of the outputted characters.

printf("%04x", 4779); // gives 12ab
printf("%04X", 4779); // gives 12AB

这篇关于printf()格式为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆