tags:

views:

84

answers:

2

if i have long long number with zeros before the number like this 0x000000000076fba1 how do i print the number with all the zeros? cuse when i tried to print the numb its writs 0x76fba1.

thank you!

+1  A: 
long long unsigned n = 0x000000000076fba1;
printf("%0x0.16llx\n", n);
Matt Joiner
Did you mean printf("0x%016llx\n", n)?
joast
well i've written it now, but they're pretty much the same after reading the docs for this case
Matt Joiner
Yep, either one will work in this case.Note: If you are using Windows, you might have to use something like printf("%0x016I64\n", n). I don't recall when MS added support for "ll" as a format specifier.
joast
thank you all i will try it. :)
bill
A: 
#ifdef __int64   
printf("%#018I64x\n", n); /* for MSVC+MinGW */
#else
printf("%#018llx\n", n);  /* other compiler with "unsigned long long" support */
#endif
There are a few gotchas with using "#" in the format. You need to account for "0x" being added to the output so you need to use 18 instead of 16 for the width. Also, a 0 (zero) value will be output without the leading "0x" (output will be "0000000000000000" if 16 is used for the width).
joast
you are right, i correct 16 -> 18
Don't use width, use precision. `"%#.16" PRIu64 "x\n"`. And especially don't litter code with such hideous `#ifdef` crap.
R..
@R: +1 for using the nice macros from `inttypes.h`
tomlogic
If you use the macros from inttypes.h, then the correct format string to get hex output is `"%#.16" PRIx64 "\n"`.It is worth noting that inttypes.h isn't available everywhere.
joast