Luis Daniel Rubiera
Luis Daniel Rubiera

Reputation: 131

How to print hexadecimal double in C?

I have this number in hexadecimal:

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; I just need to print 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; 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. If I can't printf it, neither will sprintf will work.

Upvotes: 6

Views: 16055

Answers (4)

JohnA
JohnA

Reputation: 839

An old question, but I think the OP was trying to get to a hex string representation of a double and back again.

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:

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. Also, not sure what happens with NaN and other oddball values.

Upvotes: 1

Siva
Siva

Reputation: 7

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.

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 

Upvotes: -1

unwind
unwind

Reputation: 400009

That's an integer, and a long one. Don't use double to store such a value, that's for floating-point.

Just use:

unsigned long long temp = 0xffffffffffffull;

You have 12 hexadecimal digits, so your number needs at least 12 * 4 = 48 bits. Most platforms should have an unsigned long long of 64 bits, which should be fine.

If your compiler is supported enough to support C99, you can do:

#include <stdint.h>

and then use the uint_least64_t type as suggested in a comment. 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).

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, i.e. an external representation of the number. Internally on a binary computer, the number is stored in binary.

Upvotes: 10

Jonathan Leffler
Jonathan Leffler

Reputation: 754590

While you store the value in a double, there's no sane way to get hexadecimal output.

Your code passes a double to printf() and repeatedly tries to format that as a 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).

The value is 12 F's, so it is a long long value (or 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;
}

Upvotes: 1

Related Questions