printf unsigned long long as hex

Discussion in 'C Programming' started by Schraalhans Keukenmeester, Jul 1, 2007.

  1. 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 -
    [Remove the lowercase part of Spamtrap to send me a message]

    "strcmp('apples','oranges') < 0"
    Schraalhans Keukenmeester, Jul 1, 2007
    #1
    1. Advertising

  2. 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.
    Martin Ambuhl, Jul 1, 2007
    #2
    1. Advertising

  3. Schraalhans Keukenmeester

    Flash Gordon Guest

    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
    Flash Gordon, Jul 1, 2007
    #3
  4. Schraalhans Keukenmeester <> 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) <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"
    Keith Thompson, Jul 1, 2007
    #4
  5. Schraalhans Keukenmeester

    Army1987 Guest

    "Schraalhans Keukenmeester" <> ha scritto nel messaggio
    news:77afe$4687eda0$58d3b1a3$...
    > 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... :) )
    Army1987, Jul 2, 2007
    #5
  6. Schraalhans Keukenmeester

    Army1987 Guest

    "Army1987" <> ha scritto nel messaggio news:f6ah93$oae$...
    >
    > "Schraalhans Keukenmeester" <> ha scritto nel messaggio
    > news:77afe$4687eda0$58d3b1a3$...
    >> 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"
    Army1987, Jul 2, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,135
    Neredbojias
    Aug 19, 2005
  2. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    657
    Eric Sosman
    Jul 8, 2003
  3. Joerg Schwerdtfeger

    printf / sscanf unsigned long long int variables

    Joerg Schwerdtfeger, Apr 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    40,116
    Joerg Schwerdtfeger
    Apr 2, 2004
  4. Bengt Richter
    Replies:
    6
    Views:
    448
    Juha Autero
    Aug 19, 2003
  5. pozz
    Replies:
    12
    Views:
    720
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page