HOWTO · C++

How to Convert String to Int in C++

This article explains how to convert a string to an integer in C++

On this page

To convert text to an int, use std::stoi when exception-based error handling is convenient, or std::from_chars when you want to check a result code directly. Both can parse decimal and other bases, but they handle whitespace, signs, and unconsumed characters differently.

Use std::stoi to Convert a String to an Integer in C++

The following self-contained example illustrates the ordinary decimal case:

#include <iostream>
#include <string>

int main() {
  const std::string text = "42";
  const int value = std::stoi(text);
  std::cout << value << '\n';
}
42

std::stoi is a string conversion function declared in <string>. It takes a string (or a compatible string input), an optional pointer to a std::size_t that receives the index of the first unconverted character, and an optional base. The base defaults to 10; use 2 through 36 for an explicit base, or 0 to infer a base from prefixes such as 0x.

It skips leading whitespace and accepts a leading plus or minus sign. Parsing stops at the first character that is not part of the number, so "123xyz" converts to 123. To require the whole string to be numeric, pass a position pointer and compare it with the string length. If no digits can be converted, it throws std::invalid_argument; if the value cannot fit in an int, it throws std::out_of_range.

This self-contained example demonstrates decimal, hexadecimal, full-input checking, and exception handling:

#include <iostream>
#include <stdexcept>
#include <string>

int main() {
  const std::string decimal = "-123";
  const std::string hexadecimal = "7B";
  const std::string with_suffix = "42xyz";

  try {
    const int signed_value = std::stoi(decimal);
    const int hex_value = std::stoi(hexadecimal, nullptr, 16);
    std::size_t parsed = 0;
    const int prefix_value = std::stoi(with_suffix, &parsed);

    std::cout << signed_value << '\n' << hex_value << '\n';
    if (parsed != with_suffix.size()) {
      std::cout << "Unparsed suffix: " << with_suffix.substr(parsed) << '\n';
    } else {
      std::cout << prefix_value << '\n';
    }
  } catch (const std::invalid_argument&) {
    std::cout << "Input has no integer prefix.\n";
  } catch (const std::out_of_range&) {
    std::cout << "Value is outside the int range.\n";
  }
}
-123
123
Unparsed suffix: xyz

If the conversion succeeds but leaves a suffix, the position pointer lets your program reject or separately handle that suffix. For other destination types, related functions include std::stol and std::stoll, std::stoul and std::stoull, and std::stof, std::stod, and std::stold.

Use std::from_chars to Convert a String to an Integer in C++

std::from_chars is declared in <charconv>. It parses a character range into a destination variable and returns both a pointer to the first unparsed character and an error code. Its integer overload does not skip leading whitespace or accept a leading plus sign; a leading minus sign is accepted for signed integers. For base 16, provide digits without a 0x prefix. The base defaults to 10.

The input is a half-open range [first, last), often formed from a string’s data() pointer and length. Check both fields in the result before using the destination: ec reports whether conversion succeeded, while ptr == last confirms that the complete range was consumed. A successful parse can still leave trailing characters. On std::errc::invalid_argument, no number was parsed; std::errc::result_out_of_range means the value does not fit. In either failure case, do not rely on the destination value.

This example accepts only a complete decimal value, then shows a signed hexadecimal conversion:

#include <charconv>
#include <iostream>
#include <string>
#include <system_error>

int main() {
  const std::string decimal = "-123";
  int value = 0;
  const auto decimal_result = std::from_chars(
      decimal.data(), decimal.data() + decimal.size(), value);

  if (decimal_result.ec == std::errc{} &&
      decimal_result.ptr == decimal.data() + decimal.size()) {
    std::cout << value << '\n';
  } else {
    std::cout << "Decimal input was not fully converted.\n";
  }

  const std::string hexadecimal = "7B";
  int hex_value = 0;
  const auto hex_result = std::from_chars(
      hexadecimal.data(), hexadecimal.data() + hexadecimal.size(),
      hex_value, 16);
  if (hex_result.ec == std::errc{} &&
      hex_result.ptr == hexadecimal.data() + hexadecimal.size()) {
    std::cout << hex_value << '\n';
  } else {
    std::cout << "Hexadecimal input was not fully converted.\n";
  }
}

Choose std::stoi if its exceptions and whitespace handling fit your input policy. Choose std::from_chars when you want explicit status handling and precise control over the character range. For either function, validate complete consumption when trailing text should make the input invalid.