c - 在 C 中将十六进制数打印为十进制

标签 c hex printf decimal

我正在尝试在 C 中将十六进制数字打印为十进制。

int main()
{
    unsigned long hex = 0xb5389e0c721a;
    printf("hex in dec: %lu", hex);
    getch();
    return 0;
}

由此,我的输出是 2651615770。但是,当我使用在线十六进制到十进制转换器和 Windows 计算器时,假定的输出应该是 199254774411802。我做错了什么?

最佳答案

我认为您的2651615770输出是由于溢出造成的。这是因为 32 位 unsigned long hex 无法容纳这么大的数字(0xb5389e0c721a),所以它需要更大的数据类型。

尝试以下更改-

int main()
{
    unsigned long long hex = 0xb5389e0c721a; // declare hex as unsigned long long
    printf("hex in dec: %llu", hex);
    getch();
    return 0;
}

关于c - 在 C 中将十六进制数打印为十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24971587/

相关文章:

c++ - atoi() 用于 int128_t 类型

c - 你如何使用 scanf 在 C 中获取一个 int?

c - C递归程序从数组中找到最大元素

bash - 如何使用printf打印 "-"

c++ - 将标准输出重定向到缓冲区,而不是使用 >

c - char 数组初始值设定项中的多余元素

c - C 中的位域

php - 如何在 PHP 中获取字符串的十六进制转储?

php - mt_rand() 总是给我相同的数字

c - 如何从文本文件中写入和读取(包括空格)