Thread: Int to String Functions?

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question Int to String Functions?

    Are there any int to string conversion functions?

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    itoa(int,char*,int)

    the first int is the integer to change to a string

    the char* is where the string is stored

    the second int is the radix, or the base of the the first int.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question ...

    base? what do you mean?

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Decimal is base 10, hex is base 16, binary is base 2, etc.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    itoa is non-standard.
    Use a stringstream

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    In the new faq thread on the General Disucssions board,

    Code:
    #include <sstream> //this header automatically includes iostream and is needed for ostringstream
    #include <string> 
    using namespace std;
    
    string IntToString(int num); //this will be our converter function
    
    int main(void)
    {     int number=5;
          string result=IntToString(number);
          cout<<result<<endl;
          cin.get();
          return 0;
    }
    
    string IntToString(int num)
    {     ostringstream myStream; //creates an ostringstream object
          myStream<<num<<flush; 
          /*outputs the number into the string stream and then flushes
          the buffer (makes sure the output is put into the stream)*/
          return myStream.str(); //returns the string form of the stringstream object
    }

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    Templates are your friends

    Code:
    template <typename T>
    std::string ToString(const T& number)
    {
        std::ostringstream os;
        os << number;
        return os.str();
    }

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    True, but two things...

    Is a beginner gonna know how to use templates?
    Are they needed in this instance?

    The answer to both is probably no.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Well, if the only thing being converted are int's, then no, templates aren't necessary at all, but still is good to know what your options are, don't you think?
    One should aim at making code reusable, and this was just a very humble example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM