linux - How to print hexadecimal double in C? -
i have number in hexadecimal:
ffffffffffff
and need save it, used double
double a=0xffffffffffff;
but need print , don't know how to. each time use %f
, %d
, %x
, doesn't print value of it; need print ffffffffffff
. 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 true value %f
; returns decimal value of hexadecimal — returns this:
ffffffe0 -32 ffffffe0 281474976710655.000000
with need change hexadecimal string, compare it, because have ffffffffffff in string , need compare both. if can't printf
it, neither sprintf
work.
that's integer, , long one. don't use double
store such value, that's floating-point.
just use:
unsigned long long temp = 0xffffffffffffull;
you have 12 hexadecimal digits, number needs @ least 12 * 4 = 48 bits. platforms should have unsigned long long
of 64 bits, should fine.
if compiler supported enough support c99, can do:
#include <stdint.h>
and use uint_least64_t
type suggested in comment. in linux guess you're using gcc should fine, might need specify intend code compiled c99 (with -std=c99
).
to print it, use:
printf("temp=%llx\n", temp);
also note value not hexadecimal, can print as hexadecimal. value value, base matters when converting to/from text, i.e. external representation of number. internally on binary computer, number stored in binary.
Comments
Post a Comment