How to Convert a String to an Integer in C++
Convert a string to int in C++ with std::stoi or std::from_chars. Compiled results for whitespace, signs, trailing text, overflow and invalid input.
For everyday code use std::stoi(s) (in <string>): it returns an int and throws std::invalid_argument or std::out_of_range on failure. For parsers and hot paths use std::from_chars (C++17, <charconv>): it does not throw, does not allocate, ignores locale, and reports errors through a returned error code. They differ on whitespace, + signs and partial input, as the test below shows.
#include <string>
int n = std::stoi("42"); // 42
#include <charconv>
int v = 0;
std::string_view s = "42";
auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), v);
if (ec == std::errc{}) { /* v == 42 */ }
Same inputs, both functions
We compiled one program that feeds each input to both functions (Apple clang 21, C++20) and recorded what happened:
Highlighted rows are where the two disagree or where a naive check would be fooled:
- Leading whitespace:
stoiskips it;from_charsrejects it. - Plus sign:
stoiaccepts+8;from_charsdoes not. - Trailing junk: both happily return 42 for
"42abc". You must check how much was consumed:pos == s.size()forstoi,ptr == s.data() + s.size()forfrom_chars. - Hex prefixes: neither understands
0xin base 10. Both read the0and stop.
A strict parse with each
// stoi: whole string must be a number
bool parse_stoi(const std::string& s, int& out) {
try {
std::size_t pos = 0;
out = std::stoi(s, &pos);
return pos == s.size();
} catch (const std::exception&) {
return false;
}
}
// from_chars: no exceptions, no allocation
bool parse_fc(std::string_view s, int& out) {
auto [p, ec] = std::from_chars(s.data(), s.data() + s.size(), out);
return ec == std::errc{} && p == s.data() + s.size();
}
Other bases
Both take a base argument. Our program printed stoi("ff", nullptr, 16) = 255 and from_chars("ff", base 16) = 255. stoi with base 16 also accepts a 0x prefix; from_chars does not, so strip it first. Check values with the hex to decimal converter.
Overflow and wider types
"2147483648" is one more than the largest 32-bit int, so both reported out-of-range. Use std::stol/std::stoll or from_chars into long long for bigger values; the limits are listed in the integer overflow guide.
What about atoi and stringstream?
std::atoireturns 0 for invalid input and has undefined behaviour on overflow, so you cannot tell"0"from"abc". Avoid it for untrusted input.std::istringstreamworks and is flexible, but is slower and locale-dependent;>>also stops at the first non-digit, so check for remaining characters.
The same task in Python is a one-liner with int(), compared in the Python string-to-int guide.
Sources
- std::stoi, std::stol, std::stoll — cppreference.com
- std::from_chars — cppreference.com
How we made this
Reproduced in a local lab. A test program calling std::stoi and std::from_chars on eight inputs was compiled with Apple clang 21 (-std=c++20 -O2 -Wall) and run; the results in the tables are its verbatim output.
Performed by Claude, an AI assistant working in the BinConvert repository (macOS on Apple Silicon, Apple clang 21.0.0, libc++), November 16, 2026.
This article was drafted with AI assistance. Every example, calculation and command output on this page was re-run or checked before release; the program was recompiled and its output compared with the tables. Review: automated test suite and source check, November 16, 2026.
Spotted an error? Tell us and we will correct it.