• Status: Solved
• Priority: Medium
• Security: Public
• Views: 521

# How could I convert integer to string?

Dear,

What is the function used to convert integer to string in C/C++?

currently, in delphi we have inttostr function.

Nabil.
0
nabilhammoud
2 Solutions

Freelance programmer / ConsultantCommented:
itoa and ltoa (and variants for wide strings).
0

Senior Software Engineer (Avast)Commented:
EE's very own DanRollins has written a good article on this very subject...

"Convert String to int / Convert int to String in C++"
http://www.experts-exchange.com/Programming/Languages/CPP/A_1577-Convert-String-to-int-Convert-int-to-String-in-C.html
0

Commented:
itoa may or may not be part of your system as it is not in the standard library. If you wish to do this in C++, here is an example that shows how to do it.
http://www.cplusplus.com/reference/iostream/istringstream/istringstream/
``````// using istringstream constructors.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main () {

int n,val;
string stringvalues;

stringvalues = "125 320 512 750 333";
istringstream iss (stringvalues,istringstream::in);

for (n=0; n<5; n++)
{
iss >> val;
cout << val*2 << endl;
}

return 0;
}
``````
0

Commented:
the sample phoffric provided converts string to integer. you need to use ostringstream for the reverse conversion:

std::ostringstream oss;
int i = 12345;
oss << i;
std::string si = oss.str();

if you need char array the last statement turns to

const char * pcs = oss.str().c_str();

Sara
0

Commented:
int val = 10;
char buff[10];

printf("value: %s\n", buff);

//prints value: 10
0

Author Commented:
THX
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.