In this article, we will understand how to convert an integer to a string in C++. We often come across situations wherein we need to convert the data type of the input variable from one type to another.
Convert an Integer to String in C++
The conversion of a variable or value from integer to string can be achieved using either of the following methods:
- C++ stringstream class
- C++ boost.lexical_cast() method
- C++ to_string() method
1. The stringstream class
C++ stringstream is a stream class. It connects and associates the object (string object) with the stream of the input provided.
Syntax:
stringstream object-name;
- After creating a stringstream object, we use the
<< operator
to insert the integer input value into the stream. - Further, a string type variable has to be created to store the string value of the entered integer value.
- Finally, the
>> operator
is used to read the value from the stringstream object.
Example:
#include <iostream> #include<sstream> using namespace std; int main() { int int_data; cout<<"Input an integer value..n"; cin>>int_data; stringstream obj; obj<<int_data; string string_val; obj>>string_val; cout<<"Input value after conversion from int to string:n"<<string_val; }
Output:
Input an integer value.. 24855 Input value after conversion from int to string: 24855
2. boost.lexical_cast() to Convert integer to string in C++
C++ has boost/lexical_cast.hpp library containing the boost::lexical_cast()
method to convert numbers from integer to string and string to integer.
In order to use the boost::lexical_cast() method, we will have to include the boost/lexical_cast.hpp header file in our program using the below line:
#include <boost/lexical_cast.hpp>
Syntax:
boost::lexical_cast<data-type>(variable);
Example:
#include<iostream> #include <boost/lexical_cast.hpp> #include <string> using namespace std; int main() { int int_val = 101; string x = boost::lexical_cast<string>(int_val); cout << "String representation of the input integer value:n"; cout <<x; cout<<endl; return 0; }
In the above snippet of code, the boost::lexical_cast() method has included <string> as the data type of conversion and accepted an integer value as input.
Output:
String representation of the input integer value: 101
3. to_string() function to Convert Integer to String in C++
C++ String’s to_string() method can be used to convert an input value from integer to string type.
Syntax:
to_string(value);
Example:
#include<iostream> #include <string> using namespace std; int main() { int int_val; cout<<"Enter an integer value:n"; cin>>int_val; string x = to_string(int_val); cout << "String representation of the input integer value:n"; cout <<x; cout<<endl; return 0; }
Output:
Enter an integer value: 125 String representation of the input integer value: 125
Convert String to Integer in C++
Either of the following ways can be used to convert a value from string to integer in C++:
- C++ stringstream class
- sscanf() method
- stoi() method
- atoi() method
- C++ boost::lexical_cast() method
1. The stringstream class
As mentioned earlier, C++ stringstream class associates the object (string object) with the stream of the input provided. It can be used to convert the type of the input stream from integer to string and vice-versa.
Example:
#include<iostream> #include<sstream> #include <string> using namespace std; int main() { int int_val = 0; string str_val; cout << "Enter a string value:n"; cin >> str_val; stringstream obj; obj << str_val; obj >> int_val; cout << "Integer representation of the input string value:n"; cout << int_val; return 0; }
Output:
Enter a string value: 125 Integer representation of the input string value: 125
2. C++ sscanf() Method
The sscanf() method can be used to convert string to integer in C++.
This method does not accept input from the standard stream object. Rather, it accepts input from a string variable and displays it in the integer format using an appropriate format specifier.
Syntax:
sscanf(string-variable, "%format-specifier", &int_type-variable);
Example:
#include<stdio.h> int main() { const char *str_inp = "4575"; int int_val; sscanf(str_inp, "%d", &int_val); printf("Integer representation of the input string: %d", int_val); return 0; }
In the above code, sscanf() method accepts the input from the str_inp variable of string type. Further, it uses “%d” as the format specifier to convert the string value to the integer type and stores the result in the int_val variable.
Output:
Integer representation of the input string: 4575
3. stoi() method
The stoi() function in C++ parses a string input and interprets the value of the string as an integer depending on the “base” value provided.
Syntax:
stoi(string-variable, pointer-val, base)
string-variable
: It represents the string input variable to be converted to an integer type.pointer-val (optional)
: It is a variable (pointer) to point to the character succeeding the last numeric value of the input string-variable.base (optional)
: It represents the base value to which the integer needs to be represented. The default value is base 10.
Example:
#include <iostream> #include <string> using namespace std; int main() { string str_inp1; string str_inp2; cout<<"Enter the string1:n"; cin>>str_inp1; cout<<"Enter the string2:n"; cin>>str_inp2; int int_val1 = stoi(str_inp1); int int_val2 = stoi(str_inp2); cout<<"Integer representation of String input string1:n"; cout<<int_val1<<endl; cout<<"Integer representation of String input string2:n"; cout<<int_val2<<endl; return 0; }
Output:
Enter the string1: 2578 Enter the string2: 257 Integer representation of String input string1: 2578 Integer representation of String input string2: 257
4. atoi() method
C++ atoi()
method parses the character array/c-string and interprets the string value as a value of integer type. The atoi() method returns a value of type integer.
Syntax:
atoi (const char * string-variable);
Example:
#include <iostream> #include <string> using namespace std; int main() { const char *str_inp1 = "5789"; int int_val1 = atoi(str_inp1); cout<<"Integer representation of String input:n"; cout<<int_val1<<endl; return 0; }
Output:
Integer representation of String input: 5789
Note: The only difference between atoi() and stoi() function is that the atoi() method works on character array and string literals only, while the stoi() function works on character array, string literals as well as c++ strings.
5. Boost.Lexical_Cast
Similar to how we converted an int to string using boost lexical cast above, here we will do the opposite and convert a string to int in C++.
Example:
#include<iostream> #include <boost/lexical_cast.hpp> #include <string> using namespace std; int main() { string str_val = "101"; int int_val = boost::lexical_cast<int>(str_val); cout << "Integer representation of the input string value:n"; cout <<int_val; cout<<endl; return 0; }
Output:
Integer representation of the input string value: 101
Conclusion
In this article, we have unveiled various techniques to convert an input value from integer type to string and vice-versa. A point to note for all of the examples above is that you need to handle exceptions when the values being converted are not convertible in the specified type.
For example, “Journaldev” cannot be converted to int type and you need to add methods to handle these cases in your programs.