Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf unsigned long long as hex

Reply
Thread Tools

printf unsigned long long as hex

 
 
Schraalhans Keukenmeester
Guest
Posts: n/a
 
      07-01-2007
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


--
Schraalhans Keukenmeester - http://www.velocityreviews.com/forums/(E-Mail Removed)
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-01-2007
Schraalhans Keukenmeester wrote:
> 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]

> TIA


Bite me.

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      07-01-2007
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.
--
Flash Gordon
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-01-2007
Schraalhans Keukenmeester <(E-Mail Removed)> writes:
> 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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      07-02-2007

"Schraalhans Keukenmeester" <(E-Mail Removed)> ha scritto nel messaggio
news:77afe$4687eda0$58d3b1a3$(E-Mail Removed). nl...
> 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... )


 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      07-02-2007

"Army1987" <(E-Mail Removed)> ha scritto nel messaggio news:f6ah93$oae$(E-Mail Removed)...
>
> "Schraalhans Keukenmeester" <(E-Mail Removed)> ha scritto nel messaggio
> news:77afe$4687eda0$58d3b1a3$(E-Mail Removed). nl...
>> 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... )


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

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
printf / sscanf unsigned long long int variables Joerg Schwerdtfeger C Programming 3 04-02-2004 07:03 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments