Thread: How to Convert Interger to String?

  1. #1
    Yin
    Guest

    Question How to Convert Interger to String?

    How can I convert an interger variable into a string variable? I think some of the c++ library function will do. But I cannot find it, I can only find atoi(), which does the reverse.

    An example:

    int x=99;
    char* y[100];

    How to make y to contain the string "99" ? strcpy(x,y) seems to cause error.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Use itoa

  3. #3
    Yin
    Guest
    Originally posted by Govtcheez
    Use itoa
    cannot find such thing as "itoa" in C Standard Library .... maybe my version is outdated....

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    itoa is not a standard function, you would be better off writing it yourself.
    Code:
    void reverse ( char *s )
    {
      int i, j;
      char c;
      for ( i = 0, j = strlen ( s ) - 1; i < j; i++, j-- ) {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
      }
    }
    
    void myItoa ( int n, char *s )
    {
      int i = 0, sign;
      if ( ( sign = n ) < 0 ) n = -n;
      do {
        s[i++] = (char)(n % 10 + '0');
      } while ( ( n /= 10 ) > 0 );
      if ( sign < 0 ) s[i++] = '-';
      s[i] = '\0';
      reverse ( s );
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > itoa is not a standard function, you would be better off writing it yourself.

    It's not? That's weird... K & R talks about it like it is...

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >K & R talks about it like it is...
    That's because K&R defines its own itoa. Look in the standard library reference for stdlib.h and you'll see no mention of itoa even though atof atoi atol strtod strtol and strtoul are all there. itoa is nonstandard and can be ignored by the implementation if they so wish.

    To save you some searching:
    stdlib.h is described on page 251
    itoa is defined on page 63

    -Prelude
    My best code is written with the delete key.

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    I stand corrected... I should know better than to question you anyways

    But, it's my nature (itoa's on 64)

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >itoa's on 64
    I stand corrected, I was thinking 63 because itoa was on 64 and reverse was on 62 and I kept flipping back and forth.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Yin
    Guest
    itoa is really NOT in standard c library(definitely not in ANSI-C).
    moreover, my compiler seems not supporting it.
    I better write my own, thanks prelude.

  10. #10
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    itoa is not a standard function?!?!

    *Warning....malfunction in brain*
    *Overloading....*
    *Warning...immense heat*
    *Shuting down....*
    My Website

    "Circular logic is good because it is."

  11. #11
    Yin
    Guest
    Originally posted by DavidP
    itoa is not a standard function?!?!

    *Warning....malfunction in brain*
    *Overloading....*
    *Warning...immense heat*
    *Shuting down....*
    Hey man, what so surprise?
    maybe my wording is not exact enough.
    <stdlib.h> is a general purpose standard C library, including memory allocation, process control, conversions and others.
    itoa is a function that belongs to this library. But it is not ANSI-C, however supported by most compilers.
    Mind you, atoi, in contrast, IS ANSI-C.

  12. #12
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    A standard way of doing it would be to use sprintf or a stringstream.

  13. #13
    Yin
    Guest
    Originally posted by Sorensen
    A standard way of doing it would be to use sprintf or a stringstream.
    How to use stringstream? any simple example?
    sprintf seems to be similar to cout...

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's sprintf().
    Code:
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
       int num = 123;
       char str[15];
    
       sprintf(str,"%d",num);
       cout << "str:" << str << endl;
       return 0;
    }
    I don't have stringstream with my compiler, so maybe Sorensen can show you that one. I have strstream though. I think it's almost the same, except I think the line where I have:
    strstream s(str,15,ios::out);
    It's just:
    stringstream s;
    Code:
    #include <iostream>
    #include <strstream.h>
    using namespace std;
    
    int main(void)
    {
       int num = 123;
       char str[15];
    
       strstream s(str,15,ios::out);
       s << num;
       cout << "str:" << str << endl;
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  2. convert double to string problem
    By gandalf_bar in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2004, 05:14 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03: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