Thread: How to print uint32_t variable in hex?

  1. #1
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96

    How to print uint32_t variable in hex?

    Hello,

    How can I print a uint32_t type variable in hex? Here's my code and the output:
    Code:
    /* types.h */
    #ifndef __MYTYPES_H__
    #define __MYTYPES_H__
    
    #include <stdint.h>
    #include <inttypes.h>
    
    typedef uint8_t u8;
    typedef uint16_t u16;
    typedef uint32_t u32;
    typedef uint64_t u64;
    
    #endif
    Code:
    /* mytest.c */
    #include <stdio.h>
    #include "types.h"
    
    int main() {
        printf("sizeof(u32): %zu.\n",sizeof(u32));
        printf("sizeof(PRIx16): %zu.\n",sizeof(PRIx16));
        printf("sizeof(PRIx32): %zu.\n",sizeof(PRIx32));
        printf("sizeof(PRIx64): %zu.\n",sizeof(PRIx64));
    
        return 0;
    }
    and the results:
    Code:
    sizeof(u32): 4.
    sizeof(PRIx16): 2.
    sizeof(PRIx32): 2.
    sizeof(PRIx64): 3.
    Thanks!
    Last edited by Spork Schivago; 09-21-2015 at 08:58 PM. Reason: added more code.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Use "...%x ... "

  3. #3
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    %x doesn't work. It gives a warning.
    Code:
    warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Should you include stdio.h?

  5. #5
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    I'm pretty sure I need to use the PRI macros here because I don't know a head of time what size the variable is. For example, uint32_t might be different sizes on different systems (ie, 32-bit vs 64-bit).
    Here's the output when I compile with -m32
    Code:
    sizeof(u32): 4.
    sizeof(PRIx16): 2.
    sizeof(PRIx32): 2.
    sizeof(PRIx64): 4.
    Here's the output when I don't compile with -m32
    Code:
    sizeof(u32): 4.
    sizeof(PRIx16): 2.
    sizeof(PRIx32): 2.
    sizeof(PRIx64): 3.
    I'm asking about uint32_t, but I'm using everything in my types.h header, so I need to find away to properly print without knowing the size of the printf specifiers.

  6. #6
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by Satya View Post
    Should you include stdio.h?
    Yes, I do include stdio.h, I just didn't copy and paste it into my post. I didn't think it was necessary. Sorry. It is included though.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The correct PRIx* macro is the way to go. Note that these macros are usually strings and sizeof won't tell you much that is interesting. That result is the size of the memory needed for the string.

  8. #8
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Spork Schivago View Post
    Code:
        printf("sizeof(PRIx16): %zu.\n",sizeof(PRIx16));
        printf("sizeof(PRIx32): %zu.\n",sizeof(PRIx32));
        printf("sizeof(PRIx64): %zu.\n",sizeof(PRIx64));
    Why are you printing the sizes of the printf format patterns? (Edit: Whiteflags already pointed that out; ninja'd me.)

    Examine the following instead:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    int main(void)
    {
        uint8_t  x8  = 240U;
        uint16_t x16 = ~(uint16_t)x8;
        uint32_t x32 = ~(uint32_t)x16;
        uint64_t x64 = ~(uint64_t)x32;
    
        printf("sizeof x8  = %zu\n", sizeof x8);
        printf("sizeof x16 = %zu\n", sizeof x16);
        printf("sizeof x32 = %zu\n", sizeof x32);
        printf("sizeof x64 = %zu\n", sizeof x64);
    
        printf("x8 = 0x%02" PRIx8 "\n", x8);
        printf("x16 = 0x%04" PRIx16 "\n", x16);
        printf("x32 = 0x%08" PRIx32 "\n", x32);
        printf("x64 = 0x%016" PRIx64 "\n", x64);
        printf("x64 = %" PRIu64 "\n", x64);
    
        return EXIT_SUCCESS;
    }
    Last edited by Nominal Animal; 09-21-2015 at 09:15 PM.

  9. #9
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by Nominal Animal View Post
    Why are you printing the sizes of the printf format patterns? (Edit: Whiteflags already pointed that out; ninja'd me.)

    Examine the following instead:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    int main(void)
    {
        uint8_t  x8  = 240U;
        uint16_t x16 = ~(uint16_t)x8;
        uint32_t x32 = ~(uint32_t)x16;
        uint64_t x64 = ~(uint64_t)x32;
    
        printf("sizeof x8  = %zu\n", sizeof x8);
        printf("sizeof x16 = %zu\n", sizeof x16);
        printf("sizeof x32 = %zu\n", sizeof x32);
        printf("sizeof x64 = %zu\n", sizeof x64);
    
        printf("x8 = 0x%02" PRIx8 "\n", x8);
        printf("x16 = 0x%04" PRIx16 "\n", x16);
        printf("x32 = 0x%08" PRIx32 "\n", x32);
        printf("x64 = 0x%016" PRIx64 "\n", x64);
        printf("x64 = %" PRIu64 "\n", x64);
    
        return EXIT_SUCCESS;
    }
    Thank you. So PRIx32 works for unsigned and signed? I don't have to specify that it's unsigned?

  10. #10
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    I made a mistake, actually, I've made a few. Guess it's time to call it a night and relook at things tomorrow. Just been a long day. I figure when I'm making mistakes like this, there's no sense in continuing writing code. Just gonna make more of them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-13-2015, 09:13 PM
  2. ((uint32_t)(~0UL)) ??
    By frs in forum C Programming
    Replies: 8
    Last Post: 09-18-2010, 12:21 PM
  3. how to print pointer variable
    By unikgila in forum C Programming
    Replies: 11
    Last Post: 10-27-2008, 09:49 AM
  4. u_int32_t VS uint32_t
    By meili100 in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2008, 08:58 PM
  5. uint32_t or uint32
    By RoshanX in forum C Programming
    Replies: 7
    Last Post: 03-31-2007, 06:41 AM