How to convert uint32_t to string?

This is a discussion on How to convert uint32_t to string? within the C Programming forums, part of the General Programming Boards category; Do you know how to convert uint32_t to string? Thank you very much,...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    How to convert uint32_t to string?

    Do you know how to convert uint32_t to string?

    Thank you very much,

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    char str[10];
    uint32_t num = 50;
    
    sprintf( str, "%u", num );
    Last edited by cpjust; 06-25-2008 at 05:49 PM.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Quote Originally Posted by ooosawaddee3 View Post
    Do you know how to convert uint32_t to string?

    Thank you very much,
    Another way without sprintf would be:

    Code:
    #include <stdint.h>
    #include <stdio.h>
    
    int main(void)
    {
            uint32_t y;
            uint32_t x = 50;
            unsigned char buf[4];
    
            /* pack into buf string */
            buf[0] = x >> 24;
            buf[1] = x >> 16;
            buf[2] = x >> 8;
            buf[3] = x;
    
            /* convert the string back to a uint32_t */
            y = (buf[0] <<  24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
    
            printf("y: %d = %d\n", y, x);
    
            return 0;
    }

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    977
    The easiest way is snprintf(), but don't use the %u specifier! There's no guarantee that an unsigned int is the same thing as a uint32_t. There are a couple of methods I'd suggest to do this, each of which is ugly in its own way:
    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include <inttypes.h>
    int main(void)
    {
      char str[11]; /* 11 bytes: 10 for the digits, 1 for the null character */
      uint32_t n = 12345;
      snprintf(str, sizeof str, "%lu", (unsigned long)n); /* Method 1 */
      snprintf(str, sizeof str, "%" PRIu32, n); /* Method 2 */
    }
    The first method converts the uint32_t to an unsigned long, which is guaranteed to be large enough to hold any possible uint32_t value. The second method uses a macro to get the proper conversion specifier.

    The fact that %u might work on your system is no guarantee that it will work everywhere, so please don't rely on it. The methods I showed are admittedly uglier, but they're at least portable.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mingerso View Post
    Another way without sprintf would be:

    Code:
    #include <stdint.h>
    #include <stdio.h>
    
    int main(void)
    {
            uint32_t y;
            uint32_t x = 50;
            unsigned char buf[4];
    
            /* pack into buf string */
            buf[0] = x >> 24;
            buf[1] = x >> 16;
            buf[2] = x >> 8;
            buf[3] = x;
    
            /* convert the string back to a uint32_t */
            y = (buf[0] <<  24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
    
            printf("y: %d = %d\n", y, x);
    
            return 0;
    }
    That won't become a C style string tho' - it's just the binary data stored in 4 unsigned char's - a C style string has a 0-character for termination. Your method may well end up with a zero in the middle of the character array, and is by no means sure to have a zero at the end (and there is no space for a zero either).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Wink

    Quote Originally Posted by cpjust View Post
    Code:
    char str[10];
    uint32_t num = 50;
    
    sprintf( str, "%u", num );
    use...

    Code:
    sprintf( str, "%I64u", num);

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by audinue View Post
    use...

    Code:
    sprintf( str, "%I64u", num);
    Ehm, that would almost certainly print rubbish if num is correctly defined as a 32-bit number - since it will use whatever is after the number in memory to use as the other 32-bit part of the 64-bit number. Aside from that, it's specific to Windows.

    As an example:
    Code:
    #include <stdio.h>
    
    
    int main()
    {
      unsigned int x = 7;
      printf("x = %I64d\n", x);
      return 0;
    }
    prints:
    Code:
    x = 18035564108316679
    Which doesn't look very much like 7 to me...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Quote Originally Posted by matsp View Post
    That won't become a C style string tho' - it's just the binary data stored in 4 unsigned char's - a C style string has a 0-character for termination. Your method may well end up with a zero in the middle of the character array, and is by no means sure to have a zero at the end (and there is no space for a zero either).

    --
    Mats
    Yes, you are right - I meant to mention that its another way to "put" a number into a "string" data type... but by no means the number represented as a string. Sorry for the confusion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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