Interger -> String

Started by
15 comments, last by v0dKA 20 years ago
Is there a standard function in C++ to convert an integer into a string? i have a string: string string1 = "hello"; And an integer: int integer1 = 65; I want to add the two so the final result would look like "hello65" not "helloA". I prefer, if possible, the function to be included in C++ already, not on the internet for download. ------------------------------------------------------------ .:<<-v0d[KA]->>:.
.:<<-v0d[KA]->>:.
Advertisement
For converting string->int use atoi() (in stdlib.h) which returns the integer value of the passed string, for the reverse I don''t think there is a built in function but I did find this article which gives a function for converting int->string
There is the nonstandard function itoa( int value, char *buffer, int radix )

[edited by - MikeMJH on April 3, 2004 5:40:33 PM]
This should do the job. sprintf is in stdio.h so it''s standard.

char NumberStr[16];int Number;string MyString("Adb");sprintf((char*)&NumberStr,"%d",Number);MyString.append((char*)&NumberStr);

i like to use string stream

#include <ssteam>

int your_number = 5;

char yur_numbah[10];

stringstream ss;
ss << your_number;
strcpy(your_numbah, ss.str().c_str());
FTA, my 2D futuristic action MMORPG
Another pure C++ (not C) way to do is to use stringsteams. They allow you perform all the actions on strings that you can on other streams (such as cout). Google "stringstreams" for more info.

#include <sstream>#include <string>using namespace std;int main(){   string string1 = "hello";   int integer1 = 65;   ostringstream temp;   temp << string1 << integer1;   string1 = temp.str();   return 0;} 


Another way to do it would be to overload the +operator for strings.



[edited by - yspotua on April 3, 2004 10:46:22 PM]
Thanks, that helped.

But about those sstreams... It''s apparent that if you do this:

temp << SomeString << SomeInteger;

And later on repeat that statement, it would be the equivelance of this:

temp << SomeString << SomeInteger << SomeString << SomeInteger;

But is there a clear() function that I can call on temp, kinda like temp.clear(), so that if I repeat that statement, it won''t build up but start new again. The ideal would be the following:

temp << SomeString << SomeInteger;
temp.clear();
temp << SomeString << SomeInteger;

And the repeated statement wouldn''t build up on the previous statement. Except that obviously, clear() isn''t the name of the function I''m looking for ( I experimented, but it still built up ).

Is there such a function??

..Thanks

------------------------------------------------------------
.:<<-v0d[KA]->>:.
.:<<-v0d[KA]->>:.
Yet another solution. These are functions to do int->String and String->int

_________________________________________________________________

Drew Sikora
President, Lead Programmer - Blade Edge Software
Staff Writer, Newsletter Editor - GameDev.net
Community Relations - Game Institute

Drew Sikora
Executive Producer
GameDev.net

To clear a stingstream, you can use std::stringstream::str(""). ex:
int main(int, char **) {  std::stringstream sstr;  sstr << 1 << 2;  std::cout << sstr.str() << std::endl;  sstr.str("");  sstr << 3 << 4;  std::cout << sstr.str() << std::endl;  sstr.str("");  std::cout << sstr.str() << std::endl;  return 0;}


And as an aside, I''d like to mention boost::lexical_cast, which uses std::stringstream to convert to and from different types. It helps avoid some of the more annoying errors you can get using std::stringstream for conversion.
quote:Original post by v0dKA
Thanks, that helped.

But about those sstreams... It''s apparent that if you do this:

temp << SomeString << SomeInteger;

And later on repeat that statement, it would be the equivelance of this:

temp << SomeString << SomeInteger << SomeString << SomeInteger;

But is there a clear() function that I can call on temp, kinda like temp.clear(), so that if I repeat that statement, it won''t build up but start new again. The ideal would be the following:

temp << SomeString << SomeInteger;
temp.clear();
temp << SomeString << SomeInteger;

And the repeated statement wouldn''t build up on the previous statement. Except that obviously, clear() isn''t the name of the function I''m looking for ( I experimented, but it still built up ).

Is there such a function??

..Thanks




From http://www.cplusplus.com/ref/iostream/stringstream/str.html, to clear a stringsteam object, you call the str method with and empty string.

In your previous example to clear the stream:

temp.str("");

This topic is closed to new replies.

Advertisement