如何printf的64位整数(十六进制)? [英] How to printf a 64bit integer as hex?

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

问题描述

通过以​​下code我试图输出使用 unit64_t 变量的值的printf()。编译code与海湾合作委员会,将返回以下警告:

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:

警告:格式'%X'预计类型为无符号整型的说法,   但参数2的类型为uint64_t中'[-Wformat =]

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

在code:

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

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

    return 0;
}

输出:

val = 0x90abcdef

期望的输出:

Expected output:

val = 0x1234567890abcdef

我怎么能输出64位值用十六进制整数的printf()?该 X 说明似乎错在这种情况下。

How can I output a 64bit value as a hexadecimal integer using printf()? The x specifier seems to 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.

尝试使用%LX %LLX 。欲了解更多便携性,包括 inttypes.h 并使用 PRIx64 宏。

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

例如:的printf(VAL =为0x%PRIx64\ N,VAL); (注意,这是字符串连接)

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

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

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