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

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

问题描述

我有一个数字列表如下:

I have a list of numbers as below:

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

我尝试了以下解决方案:

I have tried solution such as:

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

但我得到的结果是:

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

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

我想我离那里很近.任何人都可以帮忙,因为我不是非常擅长 C 中的 printf.

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 right justified within this width by padding on the left with the pad character. By default this is a blank space, but the leading zero we used specifies a zero as the pad char. The value is not truncated even if the result is larger.
    • x - Specifier for hexadecimal integer.
    • 更多这里

      这篇关于如何在 C 中显示十六进制数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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