如何在C显示十六进制数? [英] How to display hexadecimal numbers in C?

查看:194
本文介绍了如何在C显示十六进制数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下数字列表:

0,16,32,48 ...

0, 16, 32, 48 ...

我需要输出这些数字用十六进制为:

I need to output those numbers in hexadecimal as:

0000,0010,0020,0030,0040 ......

0000,0010,0020,0030,0040 ...

我试图解决方案,如:

printf("%.4x",a); // where a is an integer

但我得到的结果是:

but the result that I got is:

0000,0001,0002,0003 0004 ...

0000, 0001, 0002, 0003, 0004 ...

我觉得我有接近。任何人可以帮助因为我不
的printf 那么好使用C。

I think I'm close there. Can anybody help as I'm not so good at printf in C.

感谢。

推荐答案

尝试:

printf("%04x",a);


  • 0 - 左垫与号码
    零(0),而不是空间,其中
    填充指定的。

  • 4 (宽度) - 最小号
    要打印的字符。如果
    值要打印比短
    这个号码,则结果被填充
    有空格。值不
    截断即使结果是
    大。

  • X - 说明符的十六进制
    整数。

    • 0 - Left-pads the number with zeroes (0) instead of spaces, where padding is specified.
    • 4 (width) - Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
    • x - Specifier for hexadecimal integer.
    • 这篇关于如何在C显示十六进制数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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