Converting String to Number and Vice-Versa in C++

• Difficulty Level : Medium
• Last Updated : 08 Jan, 2022

In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number. But lack of knowledge of certain essential tools binds us to do so. Some methods to achieve this task are mentioned in this article.

Converting String to Number

Method 1: Using stringstream class or sscanf(): stringstream is a convenient way to manipulate strings. sscanf() is a C style function similar to scanf(). It reads input from a string rather than standard input.

Method 2: String conversion using stoi() or atoi().

• stoi() : The stoi() function takes a string as an argument and returns its value.
• atoi() : The atoi() function takes a character array or string literal as an argument and returns its value.

Method 3: Using boost lexical cast: Boost library offers an inbuild function “lexical_cast(“string”)”, which directly converts a string to a number. It returns an exception “bad_lexical_cast” in case of invalid input.

CPP

 // C++ code to demonstrate working of lexical_cast()#include // for lexical_cast()#include #include // for stringusing namespace std;int main(){    string str = "5";    string str1 = "6.5";     // Initializing f_value with casted float    // f_value is 6.5    float f_value = boost::lexical_cast(str1);     // Initializing i_value with casted int    // i_value is 5    int i_value = boost::lexical_cast(str);     // Displaying casted values    cout << "The float value after casting is : ";    cout << f_value << endl;    cout << "The int value after casting is : ";    cout << i_value << endl;     return 0;}
Output
The float value after casting is : 6.5
The int value after casting is : 5

Converting Number to String

Method 1: Using string streams: In this method, a string stream declares a stream object which first inserts a number, as a stream into an object and then uses “str()” to follow the internal conversion of number to string.

Implementation:

CPP

 // C++ code to demonstrate string stream method// to convert number to string.#include#include   // for string streams#include   // for stringusing namespace std;int main(){    int num = 2016;     // declaring output string stream    ostringstream str1;     // Sending a number as a stream into output    // string    str1 << num;     // the str() converts number into string    string geek = str1.str();     // Displaying the string    cout << "The newly formed string from number is : ";    cout << geek << endl;     return 0;}
Output
The newly formed string from number is : 2016

Method 2: Using to_string() : This function accepts a number(can be any data type) and returns the number in the desired string.

CPP

 // C++ code to demonstrate "to_string()" method// to convert number to string.#include #include // for string and to_string()using namespace std; // Driver Codeint main(){    // Declaring integer    int i_val = 20;     // Declaring float    float f_val = 30.50;     // Conversion of int into string using    // to_string()    string stri = to_string(i_val);     // Conversion of float into string using    // to_string()    string strf = to_string(f_val);     // Displaying the converted strings    cout << "The integer in string is : ";    cout << stri << endl;    cout << "The float in string is : ";    cout << strf << endl;     return 0;}
Output
The integer in string is : 20
The float in string is : 30.500000

Method 3: Using boost lexical cast: Similar to string conversion, the ” lexical_cast() ” function remains the same, but this time argument list modifies to “lexical_cast(numeric_var).

CPP

 // C++ code to demonstrate "lexical_cast()" method// to convert number to string.#include // for lexical_cast()#include #include // for stringusing namespace std; // Driver Codeint main(){    // Declaring float    float f_val = 10.5;     // Declaring int    int i_val = 17;     // lexical_cast() converts a float into string    string strf = boost::lexical_cast(f_val);     // lexical_cast() converts a int into string    string stri = boost::lexical_cast(i_val);     // Displaying string converted numbers    cout << "The float value in string is : ";    cout << strf << endl;    cout << "The int value in string is : ";    cout << stri << endl;     return 0;}
Output
The float value in string is : 10.5
The int value in string is : 17