Software

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:

CompareResults from the compiled test, per function

std::stoi

InputResult
"42"42, consumed 2 of 2 chars
" 42"42, consumed 4 of 4 chars
"-17"−17
"+8"8
"42abc"42, consumed 2 of 5 chars
"abc"throws std::invalid_argument
"2147483648"throws std::out_of_range
"0x1F"0, consumed 1 of 4 chars

std::from_chars

InputResult
"42"42, stopped after 2 of 2
" 42"errc::invalid_argument
"-17"−17
"+8"errc::invalid_argument
"42abc"42, stopped after 2 of 5
"abc"errc::invalid_argument
"2147483648"errc::result_out_of_range
"0x1F"0, stopped after 1 of 4

Highlighted rows are where the two disagree or where a naive check would be fooled:

  • Leading whitespace: stoi skips it; from_chars rejects it.
  • Plus sign: stoi accepts +8; from_chars does not.
  • Trailing junk: both happily return 42 for "42abc". You must check how much was consumed: pos == s.size() for stoi, ptr == s.data() + s.size() for from_chars.
  • Hex prefixes: neither understands 0x in base 10. Both read the 0 and 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::atoi returns 0 for invalid input and has undefined behaviour on overflow, so you cannot tell "0" from "abc". Avoid it for untrusted input.
  • std::istringstream works 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

  1. std::stoi, std::stol, std::stoll — cppreference.com
  2. 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.

Copied to clipboard!