C++ Reference ยท Strings and I/O
C++ to_chars and from_chars
Fast, locale-independent number conversion with std::to_chars and std::from_chars: error codes, bases, floating-point formats and round-trip output.
std::from_chars parses a number from a range of characters, and std::to_chars writes a number into a character buffer. Both are declared in <charconv> (C++17). They do not allocate, never throw, ignore the current locale, and are designed to be as fast as the platform allows. They report success or failure through a small result struct instead of exceptions.
They are the right tool for parsing logs, CSV fields or protocol data where bad input is common and speed matters. from_chars does not skip whitespace, does not accept a leading +, and does not accept 0x prefixes: it reads exactly the number grammar and stops.
int value = 0; auto [ptr, ec] = std::from_chars(text.data(), text.data() + text.size(), value); // ec == std::errc{} on success; ptr points after the last character consumed
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
struct from_chars_result { const char* ptr; std::errc ec; }; struct to_chars_result { char* ptr; std::errc ec; }; from_chars_result from_chars(const char* first, const char* last, /*integer*/& value, int base = 10); from_chars_result from_chars(const char* first, const char* last, /*float*/& value, chars_format fmt = chars_format::general); to_chars_result to_chars(char* first, char* last, /*integer*/ value, int base = 10); to_chars_result to_chars(char* first, char* last, /*float*/ value); to_chars_result to_chars(char* first, char* last, /*float*/ value, chars_format fmt, int precision);
| Name | Description |
|---|---|
first, last |
The character range to read, or the buffer to fill (last is one past the end). |
value |
The output number (from_chars, passed by reference) or the number to print (to_chars). Any integer type except bool; floating-point overloads exist for float, double and long double (GCC 11 and later). |
base |
2 to 36 for integers. |
fmt |
chars_format::scientific, fixed, hex or general. |
precision |
Digits after the point (or significant digits for general). |
Returns: a struct with ptr (one past the last character consumed or written) and ec. ec == std::errc{} means success; the struct converts to bool in C++26 only, so compare ec.
Errors: from_chars gives invalid_argument (no number found; value untouched) or result_out_of_range (value untouched). to_chars gives value_too_large when the buffer is too small (the buffer contents are then unspecified).
Usage notes
- No whitespace, no
+, no prefix. Trim input and handle a leading+or0xyourself. - Range check the end: a successful parse can stop early; compare
ptrwithlastif the entire field must be a number. to_charsdoes not write a terminating null. Useptr - firstas the length, as in the examples.- Locale independent. The decimal point is always
., unlikestodandprintfin some locales. - Round trip: the shortest
to_charsoverload for floating-point gives text thatfrom_charsparses back to exactly the same value. - Performance: typically several times faster than
stoi,stringstreamorprintf, with no allocation or exception cost. - Buffer size: 64 characters is enough for any integer in base 2 plus a sign; use
std::numeric_limits<T>::max_digits10as a guide for floating-point output. - For formatted, readable output use std::format; for simple throwing conversion see stoi, stod and to_string.