Thread: Integer to String

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    30

    Integer to String

    Hi all,

    We have atoi() to convert a string into an integer. How about the other way round?
    Can anyone tell me how to convert an integer into a string? Thank you..

  2. #2
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    Code:
    int num = 12345;
    char string[6];
    snprintf(string, 6 ,"%d", num);

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    yes, it's
    Code:
    char buffer[20];
    int i = 1234;
    itoa(buffer, i, 10) // 10 is the radix, base 10. try base 2 for binary representation
    //try switching 'buffer' and 'i' around if that doesn't compile. i forget the order of parameters
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Next time try providing a portable answer. Your answer is a GNU extension specific solution. Try the standard sprintf instead.
    Code:
    char buf[ BUFSIZ ] = {0};
    
    sprintf( buf, "%d", 12345 );
    On an aside, this is a very common question that, if not in the faq already, certainly gets asked on the forums all the time. Therefore, for future reference, try searching the forums (as the Announcements suggest) for answers first.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    Quote Originally Posted by quzah
    Try the standard sprintf instead.
    Didn't know that snprint wasn't portable. I also figured that it's better to use to prevent a buffer overflow.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by l0cke
    Didn't know that snprint wasn't portable. I also figured that it's better to use to prevent a buffer overflow.
    Well, to quote the man page:

    CONFORMING TO
    These are GNU extensions.
    Yes, it's better for the prevention of buffer overflows, but it's not a standard function. Just like all of the strn* functions aren't standard.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by quzah
    Just like all of the strn* functions aren't standard.
    I believe strncpy, strncat, and strncmp have been standard for a while; snprintf is standard in C99.
    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.*

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *looks* Well so they are. I guess it's time for rt to update their man pages (my quick point of reference usually).

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Integer to string?
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2006, 06:13 AM
  3. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM