How could I convert integer to string?


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

currently, in delphi we have inttostr function.

Who is Participating?
evilrixConnect With a Mentor 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++"
AndyAinscowFreelance programmer / ConsultantCommented:
itoa and ltoa (and variants for wide strings).
phoffricConnect With a Mentor 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.
// 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;

Open in new window

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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();

int val = 10;
char buff[10];
int radix = 10;

itoa(val, buff, radix);

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

//prints value: 10
nabilhammoudAuthor Commented:
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.

All Courses

From novice to tech pro — start learning today.