I thought about something like this. Is this a good way to do it ?

``````std::stringstream Num;
std::string str;

Num << 5;

str = Num.str();``````

I have this:

``int ist = 5;``

How is it possible to convert ist to std::string ?

You can make a simple function to do this using the properties of logarithms.

``````#include <iostream>
#include <cmath>
#include <string>

using namespace std;

void IntToString(int i, string & s)
{
s = "";
if (i == 0)
{
s = "0";
return;
}
if (i < 0) …``````

Write yourself a little program to test your hypothesis. It's good practice and a good learning experience.

Alternatives to using a stringstream would be sprintf(), which is C style, or itoa() which is nonstandard.

## All 7 Replies

You can make a simple function to do this using the properties of logarithms.

``````#include <iostream>
#include <cmath>
#include <string>

using namespace std;

void IntToString(int i, string & s)
{
s = "";
if (i == 0)
{
s = "0";
return;
}
if (i < 0)
{
s += '-';
i = -i;
}
int count = log10(i);
while (count >= 0)
{
s += ('0' + i/pow(10.0, count));
i -= static_cast<int>(i/pow(10.0,count)) * static_cast<int>(pow(10.0,count));
count--;
}
}

int main()
{
int i = -1024;
string s = "";
IntToString(i,s);
cout << "The string is now: " << s << endl;
system("PAUSE");
return 0;
}``````

Write yourself a little program to test your hypothesis. It's good practice and a good learning experience.

Alternatives to using a stringstream would be sprintf(), which is C style, or itoa() which is nonstandard.

One way to do it is to use std::ostringstream

``````int x = 123;
std::ostringstream osstream;
osstream << x;
std::string string_x = osstream.str();
// string_x is now "123"``````

I have found an example of sprintf() that look like this:

"For example, the following code uses sprintf() to convert an integer into a string of characters"

``````char result;
int num = 24;
sprintf( result, "%d", num );``````

Actually I dont understand the logic of this. I understand that int num = 24. This is the number. But what is char result exactly and where does the output go to a std::string ?
It look like something is missing.

Write yourself a little program to test your hypothesis. It's good practice and a good learning experience.

Alternatives to using a stringstream would be sprintf(), which is C style, or itoa() which is nonstandard.

char result; is what is called a C string, sprint f reads into the C string from number, which formats it as a string. If you don't want to use C strings, you can always use my solution that I posted =p

Yes thanks Joatm. I will take a look at that and see what could be done. :)

Do this:
If you insist on using C

``````char* result;
int num = 24;
sprintf( result, "%d", num );
std::string str(result);``````

Do this if you want to use C++

``````int someint = 24;
std::stringstream strm;
std::string num;
strm << someint;
strm >> num;``````

And avoid ever using itoa.
I find it surprising that none of the prior posts even mentioned this, it's quite basic.
I release this is a grave-dig, but the thread wasn't solved effectively.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.