We can convert string
to int
in different ways. In this article, we will discuss 5 methods to convert String to int in C++.
Table of Contents
Convert String to int in C++ using stoi()
This method was introduced in C++ 11. It is the easiest way to convert ‘string’ to ‘int’ in c++.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <string> using namespace std; //conversion using stoi() function int main() { string str1 = "12345"; string str2 = "1.9555"; string str3 = "999999"; int number1 = stoi(str1); int number2 = stoi(str2); int number3 = stoi(str3); cout << number1 << endl; cout << number2 << endl; cout << number3 << endl; return 0; } </string></iostream> |
Output:
1
999999
Note: ‘stoi()’ only returns the integer part of the decimal number without rounding off.
Convert String to int in C++ using atoi()
‘atoi()’ function can be used to convert character array or string literal to ‘int’ data type. It is defined in the ‘cstdlib’ header file.
Note:
atoi()
function will return zero if the value passed is invalid or if it encounters any other issue. ‘stoi()’ function will give the error message in that case.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> #include <cstdlib> //needed for atoi() using namespace std; //conversion using atoi() function int main() { char str1[] = "12345"; char str2[] = "1.555"; char str3[] = "999999"; char str4[] = "Java2Blog"; int number1 = atoi(str1); int number2 = atoi(str2); int number3 = atoi(str3); int number4 = atoi(str4); cout << number1 << endl; cout << number2 << endl; cout << number3 << endl; cout << number4 << endl; return 0; } </cstdlib></iostream> |
Output:
1
999999
0
Convert String to int in C++ using stringstream class
We can use stringstream
class for converting strings of digits to int. This method is useful for earlier versions of C++.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <sstream> using namespace std; int main() { string str="123456789"; //creating object of the class stringstream stringstream java2blog(str); /*object java2blog has the value of str and now stream it to the integer i*/ int i=0; java2blog >> i; //printing the value of i cout << i <<endl return></endl></sstream></iostream> |
Output:
Convert String to int in C++ using strtol() function (string to long integer)
The strtol()
method is used to convert strings to long integers. It is a function of the C standard library and can be implemented in C++. It will not consider the white space characters at the beginning of the string and it will consider other parts as numbers. It will stop when it will encounter the first character which is not a number. (It is a better option than atoi()
).
Macro Constant ERANGE=Result too large or small (out of range)
EINVAL=Invalid argument
Syntax:
1 2 3 |
long int strtol(const char *nptr, char **endptr, int base) |
Note: If the value passed for conversion is too small or too large, then it will store ERANGE in errno. If the value is too large, then it will return a value of LONG_MAX. If the value is too small, then it will return a value of LONG_MIN.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> #include <cstdlib> #include <string> using namespace std; int main() { string str={"1234567890"}; errno = 0; // set to zero(will be used to test the results) //c_str function returns a pointer to an array which contains C string //base=10 for getting a decimal number, base=16 for hexadecimal number //Explicit typecasting for converting long int to int int number = (int)strtol(str.c_str(), nullptr, 10); //CHECKING FOR ERRORS if (errno == ERANGE) { cerr << "Either Too large or too small: " << errno << "\n"; return 1; } else if (errno) { // can be EINVAL or other errors // unable to convert to a number std::cerr << "ERROR: " << errno << "\n"; return 1; } cout << "Converted string to number: "<< number << "\n"; return 0; } </string></cstdlib></iostream> |
Output:
Convert String to int in C++ using Spirit.x3 parser library of Boost
In c++, there is a header-only library Spirit.x3
of boost which can be used for parsing in C++. It can be used to parse integers with its numeric parsers. We will use the ‘boost::spirit::x3::int_parser’ for parsing the integer.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> #include <boost> using namespace std; int main(int argc, char* args[]) { namespace x3 = boost::spirit::x3; using x3::int_; string str("123456789"); // parse with boost spirit x3 int_ parser int value = 0; string::iterator strbegin = str.begin(); x3::parse(strbegin, str.end(), int_, value); cout << "INTEGER VALUE: "<<value return></value></boost></string></iostream> |
Output:
Conclusion
In this article, we discussed five different methods for how to convert string to int in C++. Sometimes it can be difficult to handle errors with atoi()
because if you take 0
as a valid input, then it will be difficult to identify if it is an error or not. For C++ 03 and earlier versions, the stringstream
method can be used.
Happy Learning!!