Writing 64 bit value in hex to a string

This is a discussion on Writing 64 bit value in hex to a string within the C Programming forums, part of the General Programming Boards category; How do I write a 64 bit value, example 0xffffffffffffffff to a buffer? My code: sprintf( buffer , "%016llx\n" , ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    Writing 64 bit value in hex to a string

    How do I write a 64 bit value, example
    0xffffffffffffffff to a buffer?
    My code:
    sprintf( buffer , "%016llx\n" , 0xffffffffffffffff ) ;

    This gives me output in buffer as 0x00000000ffffffff.

    An earlier thread suggested using %016llx or %016llX with
    printf to actually print out the output.
    But this does not seem to work with the above function.

    Please help.

    Thanks,
    Rahul.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Code:
      sprintf( buffer , "%016llx\n", 0xffffffffffffffffull ) ;
    You need to explicitly make the constant an unsigned long long
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Perhaps this?
    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    int main( void )
    {
       unsigned long long value = -1ULL;
       printf("value = 0x%016"PRIX64"\n", value);
       return 0;
    }
    
    /* my output
    value = 0xFFFFFFFFFFFFFFFF
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

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