Thread: Printing out in hex value

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    Printing out in hex value

    Im using an 8 byte value and trying to print it out using
    printf("%02X\n", variable);
    but I can only get 4 bytes to print out how do i print out all 8 bytes
    the variable is a pointer (void*)

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The printf format specifier %p is specially intended to print pointers.

    Otherwise, you could use
    Code:
    printf("%02lX\n", variable);
    %d and %x (and %o) take unsigned integers, I believe. Just as %d can print longs with an extra "l" (lowercase "L"), so can %x and %o. That's my understanding of it, anyway.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by dwks View Post
    The printf format specifier %p is specially intended to print pointers.

    Otherwise, you could use
    Code:
    printf("%02lX\n", variable);
    %d and %x (and %o) take unsigned integers, I believe. Just as %d can print longs with an extra "l" (lowercase "L"), so can %x and %o. That's my understanding of it, anyway.
    Hmm, shouldn't that be
    Code:
    printf("%02llX\n", variable);
    instead?

    EDIT: oops, I missed the part where the type is a void pointer. I was assuming a 64 bit variable on a 32 bit system.
    Last edited by bithub; 08-27-2009 at 11:54 AM.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Umm . . . yeah, oops. Also, if you're working on Windows, the runtime might not support "ll".

    The %x (hex int format) printf modifier will not work as expected on a 64-bit Windows operating system. It will only operate on the first 32 bits of the value that is passed to it.
    * Use %I32x to display an integer on a Windows 32-bit operating system.
    * Use %I64x to display an integer on a Windows 64-bit operating system.
    * The %p (hex format for a pointer) will work as expected on a 64-bit Windows operating system.
    From Common Visual C++ 64-bit Migration Issues
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Yeah, %p should be used to print pointer values.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Agreed. I like how they list the most portable solution last.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That said, in theory the pointer's value need not be printed out in hexadecimal representation when using the %p format specifier since that detail is implementation defined. Also, in other cases, if the pointer were not a pointer to void, it should be casted to pointer to void when used with %p.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing integers in hex
    By skapykat in forum C Programming
    Replies: 5
    Last Post: 10-06-2008, 02:05 PM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Printing Hex Value of a char
    By earth_angel in forum C Programming
    Replies: 6
    Last Post: 05-25-2005, 10:49 AM
  4. Printing numbers in hex format
    By miclus in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2005, 07:04 AM
  5. Printing Hex as Binary
    By Mystic_Skies in forum C Programming
    Replies: 6
    Last Post: 11-22-2004, 04:18 PM