简体   繁体   English

如何在C中打印十六进制双?

[英]How to print hexadecimal double in C?

I have this number in hexadecimal: 我有十六进制的这个数字:

FFFFFFFFFFFF FFFFFFFFFFFF

and I need to save it, so I used double 我需要保存它,所以我使用了双倍

double a=0xffffffffffff;

but I need to print it and I don't know how to. 但我需要打印它,我不知道如何。 Each time I use %f , %d , %x , it doesn't print the value of it; 每次我使用%f%d%x ,都不会打印它的值; I just need to print ffffffffffff . 我只需要打印ffffffffffff My code: 我的代码:

int main()
{
    double a=0xffffffffffff;
    printf("%x\n",a);
    printf("%d\n",a);
    printf("%X\n",a);
    printf("%f\n",a);
    return 0;
}

The only true value is %f ; 唯一真正的价值是%f ; that returns the decimal value of the hexadecimal — it returns this: 返回十六进制的十进制值 - 它返回:

 ffffffe0
 -32 
 FFFFFFE0
 281474976710655.000000

with this I need to change from that hexadecimal to string, to compare it, because I have FFFFFFFFFFFF in string too and I need to compare both. 有了这个,我需要从十六进制变为字符串,比较它,因为我也在字符串中有FFFFFFFFFFFF,我需要比较两者。 If I can't printf it, neither will sprintf will work. 如果我不能printf它, sprintf也不会工作。

That's an integer, and a long one. 这是一个整数,也是一个长整数。 Don't use double to store such a value, that's for floating-point. 不要使用double来存储这样的值,即浮点数。

Just use: 只需使用:

unsigned long long temp = 0xffffffffffffull;

You have 12 hexadecimal digits, so your number needs at least 12 * 4 = 48 bits. 您有12个十六进制数字,因此您的数字至少需要12 * 4 = 48位。 Most platforms should have an unsigned long long of 64 bits, which should be fine. 大多数平台应该有一个64位的unsigned long long整数,这应该没问题。

If your compiler is supported enough to support C99, you can do: 如果您的编译器支持足以支持C99,您可以:

#include <stdint.h>

and then use the uint_least64_t type as suggested in a comment. 然后使用评论中建议的uint_least64_t类型。 In Linux I guess you're using GCC so you should be fine, you might need to specify that you intend your code to be compiled as C99 (with -std=c99 ). 在Linux中我猜你正在使用GCC所以你应该没问题,你可能需要指定你打算将你的代码编译为C99( -std=c99 )。

To print it, use: 要打印它,请使用:

printf("temp=%llx\n", temp);

Also note that the value itself is not hexadecimal, but you can print it as hexadecimal. 另请注意,值本身不是十六进制,但您可以将其打印十六进制。 THe value is just a value, the base matters only when converting to/from text, ie an external representation of the number. 该值只是一个值,只有在转换为/从文本转换时,基数才重要,即数字的外部表示。 Internally on a binary computer, the number is stored in binary. 在二进制计算机内部,该数字以二进制形式存储。

While you store the value in a double , there's no sane way to get hexadecimal output. 当您将值存储为double ,没有理智的方法来获得十六进制输出。

Your code passes a double to printf() and repeatedly tries to format that as a plain int ; 你的代码将double传递给printf()并反复尝试将其格式化为plain int ; that is not going to work well. 这不会很好。 If you used GCC with warnings enabled, you should have got warnings about mismatches between format string and value (and if you use GCC without warnings enabled, repent your evil/lazy ways and turn the compiler warnings on — use -Wall at least, and probably -Wextra too, and fix any resulting warnings). 如果您使用GCC并启用了警告,那么您应该收到有关格式字符串和值之间不匹配的警告(如果您在未启用警告的情况下使用GCC,请重复您的邪恶/懒惰方式并打开编译器警告 - 至少使用-Wall ,并且可能是-Wextra ,并修复任何结果警告)。

The value is 12 F's, so it is a long long value (or unsigned long long ); 值为12 F,因此它是一个long long值(或unsigned long long ); store it and treat it as such: 存储它并将其视为:

int main()
{
    unsigned long long a = 0xFFFFFFFFFFFF;
    printf("%llx\n", a);
    printf("%lld\n", a);
    printf("%llX\n", a);
    printf("0x%llX\n", a);
    return 0;
}

In another context, his question is valid. 在另一个背景下,他的问题是有效的。 Say you want to print the output of the pow() function in hexadecimal - I just encountered one now. 假设您想以十六进制打印pow()函数的输出 - 我现在刚刚遇到一个。

printf( "pow(a, b) is : %f\n", pow(a, b) );  //Will give decimal value 
printf( "pow(a, b) is : %X\n", pow(a, b) );  //Not allowed 

So do 那样做

unsigned long long int temp; 
temp = pow(a, b); 
printf( "pow(a, b) is : %X\n", temp);       //Will give hexadecimal value 

An old question, but I think the OP was trying to get to a hex string representation of a double and back again. 一个老问题,但我认为OP试图获得双重的十六进制字符串表示并再次返回。

If so, here's some code to do that: 如果是这样,这里有一些代码:

union {
    double dbl;
    uint8_t i8[sizeof(double) + 1];
} buf;

printf("sizeof(dbl) = %ld\n", sizeof(double));
memset(buf.i8, 0x00, sizeof(buf.i8));
buf.dbl = 3.14159265359;
printf("%f\n", buf.dbl);
for(int i = 0; i < (int) sizeof(buf.i8) - 1; ++i) {
    printf("%02X", buf.i8[i]);
}
printf("\n");

The resulting output is: 结果输出是:

sizeof(dbl) = 8
3.141593
EA2E4454FB210940

To read in the 16 character string and get the double back: 要读入16个字符串并获得双后退:

const char* str = "EA2E4454FB210940";
int j = 0;
for (int i = 0; i < (int) strlen(str); i += 2) {
    uint8_t val = 0;
    for (int k = 0; k < 2; ++k) {
        char ch = str[i + k];

        val *= 16;
        if (ch >= '0' && ch <= '9') {
            val += ch - '0';
        } else if (ch >= 'A' && ch <= 'F') {
            val += ch - 'A' + 10;
        }
    }
    buf.i8[j++] = val;
}

for(int i = 0; i < (int) sizeof(buf.i8) - 1; ++i) {
    printf("%02X", buf.i8[i]);
}
printf("\n");

printf("%f\n", buf.dbl);

The output is: 输出是:

EA2E4454FB210940
3.141593

Note that this is on OSX, different platforms may not have the same binary representation for a double. 请注意,这是在OSX上,不同的平台可能没有相同的二进制表示形式。 Also, not sure what happens with NaN and other oddball values. 此外,不确定NaN和其他奇怪的值会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM