In this C++ tutorial, you will learn how to convert a string value into an integer value using stoi(), atoi(), or stringstream() functions, with example programs.
C++ String to Integer
We can convert a string to integer in C++ in many ways.
To convert a string to integer, we can use stoi() function, atoi() function or stringstream.
Methods
1. Convert String to Integer using stoi()
To convert a string to integer in C++, you can use stoi() function. The function template of stoi() is given below.
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
stoi() parses str (first argument) as a number and returns an integer.
You can also pass the base (octa, deci, hex, etc.,) using which you would like parse the string to integer.
In the following example, we shall use stoi() function to convert a string to integer.
main.cpp
#include <iostream>
using namespace std;
int main() {
string str1 = "512";
int n = stoi(str1);
cout << n << Lendl;
}
Output
512
Program ended with exit code: 0
Now let us take a string with some number and parse it into integer with a base of 8.
We shall pass 8
for base (third argument), and nullptr
(null pointer) for idx(second argument).
main.cpp
#include <iostream>
using namespace std;
int main() {
string str1 = "512";
int n = stoi(str1, nullptr, 8);
cout << n << endl;;
}
Output
330
Program ended with exit code: 0
Explanation
512 base 8 = 5*(8*8) + 1*(8) + 2(1)
= 320 + 8 + 2
= 330
You can also pass a string with some text and numbers. stoi() detects the number and returns.
In the following program, we have covered different such scenarios.
main.cpp
#include <iostream>
using namespace std;
int main() {
cout << stoi("3.14253") << endl; //returns only 3 leaving out the rest
cout << stoi("3523 hello") << endl; //returns number 3523
cout << stoi("568 hello 536 sks") << endl; //returns first found number 568
}
Output
3
3523
568
Program ended with exit code: 0
2. Convert String to Integer using atoi()
atoi() function converts a character array to integer. works much like stoi() but takes char array as argument.
In the following example, we shall use atoi() function to convert a char array to integer.
main.cpp
#include <iostream>
using namespace std;
int main() {
char str[] = "512";
int n = atoi(str);
cout << n;
}
Output
512
Program ended with exit code: 0
Let us take strings containing different values like, one having float value, the next one having a number with some text, etc.
main.cpp
#include <iostream>
using namespace std;
int main() {
cout << atoi("3.14253") << endl; //returns only 3 leaving out the rest
cout << atoi("3523 hello") << endl; //returns number 3523
cout << atoi("568 hello 536 sks") << endl; //returns first found number 568
}
Output
3
3523
568
Program ended with exit code: 0
3. Convert String to Integer using stringstream()
To convert a string to integer using stringstream() function, create a stringstream with the string, and stream it to integer.
main.cpp
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string str = "314";
int n;
stringstream(str) >> n;
cout << n;
}
Output
314
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned how to convert a string to integer, in many different ways, using functions like stoi(), atoi(), stringstream(), etc., with the help of example C++ programs.