【问题标题】:Printing out numbers as hexidecimal numbers将数字打印为十六进制数字
【发布时间】:2013-12-13 22:03:00
【问题描述】:

所以我有数字 12 我如何以格式打印出来

0x0000000C // where there is always the 0x in the beginning and always 8 digits after to 
           // represent the number

fprintf(outputFile, "%x", 12);

给我:

cc

但我想要:

0x0000000C

【问题讨论】:

    标签: c printf hex


    【解决方案1】:

    %08x

    会给你正确的位数。只需将0x 放在前面作为装饰即可。

    %08X 会将字母打印为大写字母。

    【讨论】:

      【解决方案2】:

      需要加前缀 0x 然后大写 X 得到大写 C 和 08 表示前缀加 0 最多 8 个字符

      fprintf(outputFile, "0x%08X", 12);
      

      【讨论】:

        【解决方案3】:

        试试这个

        #include<stdio.h>
        void main()
        {
        int x;
        x=12;
        
        printf("%#010x\n",x);
        }
        

        【讨论】:

          【解决方案4】:
          fprintf(outputFile, "%#.8x", 12);
          

          # 标志指定备用格式,它在0x 前面加上x 格式。 .8 指定精度为 8,这是 x 格式出现的最小位数。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-18
            • 1970-01-01
            • 2015-12-16
            • 2012-05-22
            • 2014-09-18
            相关资源
            最近更新 更多