DEV Community

Lalit Kumar
Lalit Kumar

Posted on

How to convert integer to string in C++

Methods are given below:

  1. Do not use theitoa or functions itofas they are non-standard and therefore not portable.
  2. Use string streams
 #include <sstream>  //include this to use string streams
 #include <string> 

int main()
{    
    int number = 1234;

    std::ostringstream ostr; //output string stream
    ostr << number; //use the string stream just like cout,
    //except the stream prints not to stdout but to a string.

    std::string theNumberString = ostr.str(); //the str() function of the stream 
    //returns the string.

    //now  theNumberString is "1234"  
}
Enter fullscreen mode Exit fullscreen mode

Note that you can use string streams also to convert floating point numbers to a string, and format the string as you wish, just like with cout

std::ostringstream ostr;
float f = 1.2;
int i = 3;
ostr << f << " + " i << " = " << f + i;   
std::string s = ostr.str();
//now s is "1.2 + 3 = 4.2" 
Enter fullscreen mode Exit fullscreen mode

title: "originally posted here 👇"

canonical_url: https://kodlogs.com/blog/2289/how-to-convert-integer-to-string-in-c-in-c-03

You can use stream manipulators like std::endl, std::hex and functions std::setw(), std::setprecision() etc. with string streams just like with cout

Do not confuse std::ostringstream with std::ostrstream. The latter is obsolete

Use boost lexical casting. If you are not familiar with boost, it is a good idea to start with a small library like this lexical_cast. You can download and install boost and its documentation here . Although boost is not part of the C ++ standard, many boost libraries are eventually standardized, and boost is widely regarded as one of the best C ++ libraries.

Lexical casting uses streams at the bottom, so this is basically the same as the previous one, only less verbose.

#include <boost/lexical_cast.hpp>
#include <string>

int main()
{
   float f = 1.2;
   int i = 42;
   std::string sf = boost::lexical_cast<std::string>(f); //sf is "1.2"
   std::string si = boost::lexical_cast<std::string>(i); //sf is "42"
}
Enter fullscreen mode Exit fullscreen mode

How to convert string to number in C ++ 03:(Method 2):

The easiest option, inherited from C, is functions atoi (for integers (alphabet to integer)) and atof (for floating point values ​​(alphabet to float)). These functions take a C-style ( const char *) string as an argument , and therefore may not be considered good C ++ practice to use them . cplusplus.com has easy-to-understand documentation for both atoi and atof, including how they behave in the event of bad input. However, the reference contains the error that, according to the standard, if the input number is too large to fit into the target type, the behavior is undefined.

#include <cstdlib> //the standard C library header
#include <string>
int main()
{
    std::string si = "12";
    std::string sf = "1.2";
    int i = atoi(si.c_str()); //the c_str() function "converts" 
    double f = atof(sf.c_str()); //std::string to const char*
}
Enter fullscreen mode Exit fullscreen mode

Use string streams (this time input string stream, istringstream). Again, istringstream is used in the same way as cin. Again, do not be confused istringstream with istrstream. The latter is already obsolete.

#include <sstream>
#include <string>
int main()
{
   std::string inputString = "1234 12.3 44";
   std::istringstream istr(inputString);
   int i1, i2;
   float f;
   istr >> i1 >> f >> i2;
   //i1 is 1234, f is 12.3, i2 is 44  
}
Enter fullscreen mode Exit fullscreen mode

Use boost lexical casting.

#include <boost/lexical_cast.hpp>
#include <string>

int main()
{
   std::string sf = "42.2"; 
   std::string si = "42";
   float f = boost::lexical_cast<float>(sf); //f is 42.2
   int i = boost::lexical_cast<int>(si);  //i is 42
}
Enter fullscreen mode Exit fullscreen mode

In case of incorrect input, lexical_cast throws an exception of the type boost::bad_lexical_cast
Read more in original post

Top comments (0)