C and C++ converting string to integer or floating point type and vice versa


In this post let us see some of the ways to convert string to int type or floating point type and vice versa.The discussion includes:

i) C++ way to convert string to int or floating point type and
ii) C way to convert string to int or float type

(Using Boost library to convert string to int or float type will be also discussed at the end of the post)

Besides converting string to int or float type we will also see how to perform the vice versa:

i) C++ way to convert int or floating point to string type and
ii) C way to convert int or float to string type

(And off course how to achieve it using the Boost library will be also shown)



C++ way to convert string to int or floating point type

In C++ we will use the istringstream type variable to store the string that is to be converted to int or floating point type and push the content of the istringstream variable to the int or floating point variable.The code below shows how to do it.

#include <sstream>
#include <iostream>

using namespace std;

int main( )
{
istringstream si(“1090”) ,
sf(“12.435”) ,
s1(“Not a number”) ; //a non-convertible string to int/foat is passed

int i ;
float f , ff ;

si >> i ; //assigns the value of si to ‘i’
sf >> f ; //assigns the value of sf to ‘f’
s1 >> ff ; //assigns the value of s1 to ‘ff’

cout<< i << endl
<< f << endl
<< ff ;

cin.get( );
return 0;
}

Output

1090
12.435
0

The string s1 is not convertible to float type so ‘0’ is returned.






C way to convert string to int or floating point type

In C the <stdlib.h> library provide some functions to convert string to int or floating point type.We can use functions like strtol:converts string to long int type, strtod:converts string to double type.Note these functions can be also used in C++ as <stdlib.h> is part of C++,but alter the library name to <cstdlib>.

A brief description of the function

strtol will accept three arguments :
first argument – A string to be converted to long int type.If the string cannot be converted to int/float type 0 is returned.
second argument – A char** type .
third argument – also known as base value in our case we will use 10.

strtod will accept two arguments :
first argument – A string to be converted to double type.If the string cannot be converted to floating point type 0 is returned.
second argument – A char** type .

Link: C strof,strtod and strtold

A code example is given below.

#include <stdio.h>
#include <math.h>

int main( )
{
char *pt;

long int i=strtol(“2900” , &pt , 10) ,
ival=strtol(“A random string” , &pt , 10);

double d=strtod(“23.4789” , &pt) ,
dnan=strtod(“nan” , &pt) ; ///a ‘nan’ type value,stands for “Not a Number”

printf( “%i \n %i \n\n %lf \n %lf” , i , ival , d , dnan );

getchar( );
return 0;
}

Output

2900
0

23.4789
nan






C++ way to convert int or floating point to string type

Converting integer or floating point value to string type in C++ utilizes the same concept as converting string to int/floating point type.However,here we will use stringstream type to hold the integer or floating point value and the stringstream variable is assigned to the ‘string’ variable.A code example is given below.

#include <sstream>
#include <iostream>

using namespace std;

int main( )
{
stringstream ss , ssinf ;
string sn , sinf ;

ss<< 100 ;

ss >> sn ;

ssinf<< INFINITY ; //INFINITY is a macro for infinity value

ssinf >> sinf ;

cout << sn << endl
<< sinf ;

cin.get( );
return 0;
}

Output

100
inf


 




C way to convert int or floating point to string type

In C to convert integer or floating point value to string type we will use the function sprintf included under the header file stdio.h.Note it can be also used in C++ but change the header name to cstdio.

The function sprintf will accept three arguments :
first argument – A char buffer to store the string that is converted from int or floating point type.
second argument – A format of the third argument type supplied .
third argument – An integer or floating point value to be converted to string.

A code example is provided below.

#include <stdio.h>
#include <math.h>

int main( )
{
char buf[10];

sprintf( buf , “%f” , 100.89 );
printf( “\n%.10s ” , buf) ;

sprintf( buf , “%f” , 23.4e5 );
printf( “\n%.10s ” , buf) ;

sprintf( buf , “%f” , INFINITY ); //INFINITY is a macro to represent an infinity value
printf( “\n%.10s ” , buf) ;

getchar( );
return 0;
}

Output

100.890000
2340000.00
inf
 


*Side Note

Using boost we can easily convert string to int type or vice versa.

 1Converting string to integer or floating point value:
In Boost we use the boost::lexical_cast function to convert string to int type.In using this function the type to be converted to is mentioned inside the opening and closing angle bracket appended to the function.An example is provided below.

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

int i = boost::lexical_cast<int>(“100”);

double d=boost::lexical_cast<double>(“100.536”);

cout<< i << endl
<< d ;

You can guess the output yourself.

  2Converting string to integer or floating point value:
Also to convert int or floating point value to string type we again use the boost::lexical_cast function.A code example is provided below.

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

double i = boost::lexical_cast<string>(23.456) ,
nan= boost::lexical_cast<string>( NAN ) ,
infinity= boost::lexical_cast<string>( INFINITY ) ;

cout<< i << endl
<< nan << endl
<< infinity ;

For this one also you can guess the output yourself.