如何打印一个64位整数为十六进制? [英] How to printf a 64-bit integer as hex?

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

问题描述

使用下面的代码,我试图使用 printf()输出 unit64_t 变量的值。使用gcc编译代码会返回以下警告:
$ b


warning:格式'%x'需要类型为'unsigned int'的参数,
但参数2的类型为'uint64_t'[-Wformat =]


代码:

  #include< stdio.h> 
#include< stdint.h>

int main()
{
uint64_t val = 0x1234567890abcdef;
printf(val = 0x%x \ n,val);

返回0;





输出:

< pre $ val = 0x90abcdef

预期输出:

  val = 0x1234567890abcdef 

如何使用 printf()输出64位值作为十六进制整数?在这种情况下, x 说明符似乎是错误的。

解决方案

来自编译器的警告告诉您,您的格式说明符与您传递给它的数据类型不匹配。



尝试使用%lx %llx 。为了更加便携,请包含 inttypes.h 并使用 PRIx64 宏。



例如: printf(val = 0x%PRIx64\\\
,val);
(注意它是字符串连接)


With the following code I am trying to output the value of a unit64_t variable using printf(). Compiling the code with gcc, returns the following warning:

warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘uint64_t’ [-Wformat=]

The code:

#include <stdio.h>
#include <stdint.h>

int main ()
{
    uint64_t val = 0x1234567890abcdef;
    printf("val = 0x%x\n", val);

    return 0;
}

The output:

val = 0x90abcdef

Expected output:

val = 0x1234567890abcdef

How can I output a 64bit value as a hexadecimal integer using printf()? The x specifier seems to be wrong in this case.

解决方案

The warning from your compiler is telling you that your format specifier doesn't match the data type you're passing to it.

Try using %lx or %llx. For more portability, include inttypes.h and use the PRIx64 macro.

For example: printf("val = 0x%" PRIx64 "\n", val); (note that it's string concatenation)

这篇关于如何打印一个64位整数为十六进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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