C语言 unsigned long long 类型以十六进制格式打印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19284978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:41:09  来源:igfitidea点击:

unsigned long long type printing in hexadecimal format

cintegerprintf

提问by Paul the Pirate

I am trying to print out an unsigned long longlike this:

我正在尝试打印出unsigned long long这样的:

  printf("Hex add is: 0x%ux ", hexAdd);

but I am getting type conversion errors since I have an unsigned long long.

但由于我有一个unsigned long long.

回答by gavinb

You can use the same llsize modifier for %x, thus:

您可以对 使用相同的ll大小修饰符%x,因此:

#include <stdio.h>

int main() {
    unsigned long long x = 123456789012345ULL;
    printf("%llx\n", x);
    return 0;
}

The full range of conversion and formatting specifiers is in a great table here:

完整的转换和格式说明符在一个很棒的表格中:

回答by I?ya Bursov

try %llu- this will be long long unsigned in decimal form

尝试%llu- 这将是十进制形式的 long long unsigned

%llxprints long long unsigned in hex

%llx以十六进制打印 long long unsigned

回答by Joseph

printf("Hex add is: %llu", hexAdd);

回答by Cool Javelin

I had a similar issue with this using the MinGW libraries. I couldn't get it to recognize the %llu or %llx stuff.

我在使用 MinGW 库时遇到了类似的问题。我无法让它识别 %llu 或 %llx 的东西。

Here is my answer...

这是我的答案...

void PutValue64(uint64_t value) {
char    a_string[25];   // 16 for the hex data, 2 for the 0x, 1 for the term, and some spare
uint32  MSB_part;
uint32  LSB_part;

    MSB_part = value >> 32;
    LSB_part= value & 0x00000000FFFFFFFF;

    printf(a_string, "0x%04x%08x", MSB_part, LSB_part);
}

Note, I think the %04x can simply be %x, but the %08x is required.

请注意,我认为 %04x 可以简单地为 %x,但 %08x 是必需的。

Good luck. Mark

祝你好运。标记