How do I print 64 bit unsigned integer in hex?

This is a discussion on How do I print 64 bit unsigned integer in hex? within the C Programming forums, part of the General Programming Boards category; I wonder how I use printf to print an unsigned 64 bit integer in hex? I want to print all ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Red face How do I print 64 bit unsigned integer in hex?

    I wonder how I use printf to print an unsigned 64 bit integer in hex? I want to print all positions, even if they are zero like this:

    0000000000000000

    or:

    00FFFFFFFFFFFFFF

    or:

    000C5AB7643DAA4F
    You cantīt teach an old dog new tricks.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    printf() format modifiers maybe. This is the code for a regular int:
    >printf ("%010d\n", 9);
    The first 0 of 010 tells the compiler to print leading 0's.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    printf("%016X\n",your_num);

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Wink

    Originally posted by penney
    printf("%016X\n",your_num);
    This does not work. When building I get a warning:

    unsigned int format, difference type arg (arg 2)

    And the resulting output is:

    00000000FFFFFFFF

    This is my code:

    UInt64 your_num = 0xffffffffffffffff;
    printf("%016X\n", your_num);

    What is wrong?
    You cantīt teach an old dog new tricks.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Try:
    Code:
    printf("%016llX\n", your_num);

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Talking

    Originally posted by Monster
    Try:
    Code:
    printf("%016llX\n", your_num);
    IT WORKS!!!

    Thanks a lot my friend. No build error and the output is exactly as it should. I own you one :-)

    What have this learned me? Well, even Monster's can be nice, heh...
    You cantīt teach an old dog new tricks.

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by electrolove
    IT WORKS!!!

    Thanks a lot my friend. No build error and the output is exactly as it should. I own you one :-)

    What have this learned me? Well, even Monster's can be nice, heh...
    It's from cookie monster.... I love cookies

    b.t.w. I'm not sure if this is ANSI C

  8. #8
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Originally posted by Monster
    b.t.w. I'm not sure if this is ANSI C
    As far as I understand the ANSI C Standard, I would say "%016llX" is ANSI C conform.
    0 For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, leading zeros
    (following any indication of sign or base) are used to pad to the field width rather
    than performing space padding, except when converting an infinity or NaN. If the
    0 and - flags both appear, the 0 flag is ignored. For d, i, o, u, x, and X
    conversions, if a precision is specified, the 0 flag is ignored. For other
    conversions, the behavior is undefined.
    ...
    ll (ell-ell) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a
    long long int or unsigned long long int argument; or that a
    following n conversion specifier applies to a pointer to a long long int
    argument.
    ...
    o,u,x,X The unsigned int argument is converted to unsigned octal (o), unsigned
    decimal (u), or unsigned hexadecimal notation (x or X) in the style dddd; the
    letters abcdef are used for x conversion and the letters ABCDEF for X
    conversion. The precision specifies the minimum number of digits to appear;
    if the value being converted can be represented in fewer digits, it is expanded
    with leading zeros. The default precision is 1. The result of converting a
    zero value with a precision of zero is no characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit or 64 bit allignment ?! gcc options??
    By mynickmynick in forum C Programming
    Replies: 3
    Last Post: 07-29-2008, 03:43 AM
  2. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 02:26 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 09:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21