• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 511
  • Last Modified:

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
Asked:
nabilhammoud
2 Solutions
 
AndyAinscowCommented:
itoa and ltoa (and variants for wide strings).
0
 
evilrixSenior 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
 
phoffricCommented:
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;
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sarabandeCommented:
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
 
tomnovCommented:
int val = 10;
char buff[10];
int radix = 10;

itoa(val, buff, radix);

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

//prints value: 10
0
 
nabilhammoudAuthor Commented:
THX
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now