printf unsigned long long as hex

  • Thread starter Schraalhans Keukenmeester
  • Start date
S

Schraalhans Keukenmeester

Hi,

printf's format specifier %X only prints values of up to 8 hex digits.
I am using GCC on Gentoo Linux. (How do I find which GCC version I have?)

Is it possible to print values as hex up to 15 or 16 hex digits? If not,
a workaround would be appreciated also.
TIA
 
M

Martin Ambuhl

Schraalhans said:
Hi,

printf's format specifier %X only prints values of up to 8 hex digits.
I am using GCC on Gentoo Linux. (How do I find which GCC version I have?)

gcc questions, like all implementation-specific questions, belong in an
implementation-specific newsgroup, but only after checking the supplied
documentation. As it happens, your documentation adequately explains
the switches available in invoking gcc. Check it.
Is it possible to print values as hex up to 15 or 16 hex digits? If not,
a workaround would be appreciated also.

#include <stdio.h>
#include <limits.h>

int main(void)
{
unsigned char uc = UCHAR_MAX;
unsigned short us = USHRT_MAX;
unsigned int ui = UINT_MAX;
unsigned long ul = ULONG_MAX;
unsigned long long ull = ULLONG_MAX;
puts("[output, specific values are implementation-dependent.]");

printf("UCHAR_MAX, %u (decimal), %#o (octal), %#x (hex)\n",
uc, uc, uc);


printf("USHRT_MAX, %u (decimal), %#o (octal), %#x (hex)\n",
us, us, us);

printf("UINT_MAX, %u (decimal), %#o (octal),\n %#x (hex)\n",
ui, ui, ui);

printf("ULONG_MAX, %lu (decimal), %#lo (octal),\n %#lx (hex)\n",
ul, ul, ul);

printf
("ULLONG_MAX, %llu (decimal),\n %#llo (octal),\n %#llx (hex)\n",
ull, ull, ull);

puts("[end of output]");
return 0;
}

[output, specific values are implementation-dependent.]
UCHAR_MAX, 255 (decimal), 0377 (octal), 0xff (hex)
USHRT_MAX, 65535 (decimal), 0177777 (octal), 0xffff (hex)
UINT_MAX, 4294967295 (decimal), 037777777777 (octal),
0xffffffff (hex)
ULONG_MAX, 4294967295 (decimal), 037777777777 (octal),
0xffffffff (hex)
ULLONG_MAX, 18446744073709551615 (decimal),
01777777777777777777777 (octal),
0xffffffffffffffff (hex)
[end of output]

Bite me.
 
F

Flash Gordon

Schraalhans Keukenmeester wrote, On 01/07/07 19:08:
Hi,

printf's format specifier %X only prints values of up to 8 hex digits.

Then presumably that is the number of digits required to represent an
unsigned int on your implemention.
I am using GCC on Gentoo Linux. (How do I find which GCC version I have?)

The correct place to ask that is your man/info pages of gnu.gcc.help,
but I'm feeling generous, so I suggest you try 'gcc -v'.
Is it possible to print values as hex up to 15 or 16 hex digits? If not,
a workaround would be appreciated also.

Use the correct format specifier for the type you are printing. 'man 3
printf' or your textbook will give you lots of information (if you don't
have a textbook I strongly suggest buying K&R2 (search the group for
full details), but depending on the details yout might want %lX or %llX,
but note that the value you pass should be of type unsigned whatever.
 
K

Keith Thompson

Schraalhans Keukenmeester said:
printf's format specifier %X only prints values of up to 8 hex digits.
I am using GCC on Gentoo Linux. (How do I find which GCC version I have?)

Questions about gcc are off-topic here. You'll have to post to
gnu.gcc.help, or consult the extensive documentation that comes with
gcc, to learn about "gcc --version".
Is it possible to print values as hex up to 15 or 16 hex digits? If not,
a workaround would be appreciated also.

As it happens, gcc is a compiler, not a full implementation. The
printf function is part of the runtime library, not of the compiler,
so knowing what version of gcc you have doesn't necessarily help you
to find out what printf support.

The sizes of the predefined integer types can vary from one
implementation to another; the standard merely specifies minimum
required ranges and relationships. There are plenty of
implementations where unsigned long is 64 bits; on such systems;
"%016lx" will print 16 hexadecimal digits.

In many implementations, unsigned long long is 64 bits, but that type
was added to the C standard only in 1999, and few implementations
fully support the C99 standard. (It's legal for unsigned long long to
be bigger than 64 bits, but I've never heard of such an
implementation.) The format for printing an unsigned long long value
in 16 hexadecimal digits, with leading zeros as needed, is "016llx"
(that's zero one six ell ell ex for those reading with fonts that
don't distinguish between '1' and 'l'). But this will work only if
(a) your compiler supports unsigned long long, and (b) your runtime
library supports the format. It's likely to work on your
implementation, but be aware that it could break on other systems.
 
A

Army1987

Schraalhans Keukenmeester said:
Hi,

printf's format specifier %X only prints values of up to 8 hex digits.
I am using GCC on Gentoo Linux. (How do I find which GCC version I have?)

Is it possible to print values as hex up to 15 or 16 hex digits?
"%.15X" or "%.16X"
If not,
a workaround would be appreciated also.

"00000000%X" (I hope you get the point... :) )
 
A

Army1987

Army1987 said:
"%.15X" or "%.16X"
If not,

"00000000%X" (I hope you get the point... :) )

long... long...
I had missed one "long"...

Ignore completely what I've written above...
"%llX"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top