How could I convert integer to string?

Posted on 2011-03-02
Last Modified: 2012-05-11

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

currently, in delphi we have inttostr function.

Question by:nabilhammoud
LVL 44

Expert Comment

ID: 35024590
itoa and ltoa (and variants for wide strings).
LVL 40

Accepted Solution

evilrix earned 125 total points
ID: 35024710
EE's very own DanRollins has written a good article on this very subject...

"Convert String to int / Convert int to String in C++"
LVL 32

Assisted Solution

phoffric earned 125 total points
ID: 35024718
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

LVL 33

Expert Comment

ID: 35025725
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();


Expert Comment

ID: 35025776
int val = 10;
char buff[10];
int radix = 10;

itoa(val, buff, radix);

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

//prints value: 10

Author Closing Comment

ID: 35025910

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question