Solved

How could I convert integer to string?

Posted on 2011-03-02
6
481 Views
Last Modified: 2012-05-11
Dear,

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

currently, in delphi we have inttostr function.

Nabil.
0
Comment
Question by:nabilhammoud
6 Comments
 
LVL 44

Expert Comment

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

Accepted Solution

by:
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++"
http://www.experts-exchange.com/Programming/Languages/CPP/A_1577-Convert-String-to-int-Convert-int-to-String-in-C.html
0
 
LVL 32

Assisted Solution

by:phoffric
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.
      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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Expert Comment

by:sarabande
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();

Sara
0
 

Expert Comment

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

itoa(val, buff, radix);

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

//prints value: 10
0
 

Author Closing Comment

by:nabilhammoud
ID: 35025910
THX
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now