Archived

This topic is now archived and is closed to further replies.

v0dKA

Interger -> String

Recommended Posts

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]->>:.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
There is the nonstandard function itoa( int value, char *buffer, int radix )

[edited by - MikeMJH on April 3, 2004 5:40:33 PM]

Share this post


Link to post
Share on other sites
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);


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]->>:.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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("");

Share this post


Link to post
Share on other sites
writing a lexical_class template isn''t hard. Check this out:


#include <string>
#include <sstream>

template<typename R, typename T>
R lexical_cast(T const& val)
{
R ret;
std::stringstream st;
if(!(st<<val) || !(st>>ret)) {}// /* No bad_cast in stlport? */ throw std::bad_cast();

return ret;
}

Share this post


Link to post
Share on other sites
quote:
Original post by Deyja
writing a lexical_class template isn''t hard. Check this out:
(snip)
There is one complication. What does this line do?
std::cout <<lexical_cast<std::string>("hi mom") << std::endl;

Share this post


Link to post
Share on other sites
Really? What compiler/stl implementation?

It printed a nice hex number for me. I suppose one of us has a non-standard overload for stringstream<

Share this post


Link to post
Share on other sites
GCC 3.2 (and its supplied library implementation).

The issue which I meant to bring up was that it only reads the first token off, so casting to a string can lose information. Due to lack of compiler support for partial template specialisation, it can take a bit of trickery to get it working. The boost team can save you this trouble.

Share this post


Link to post
Share on other sites
quote:
Original post by Deyja
Really? What compiler/stl implementation?

It printed a nice hex number for me. I suppose one of us has a non-standard overload for stringstream<

You do. See 27.6.2.5.4 in the C++ standard.

Share this post


Link to post
Share on other sites