# C++ String Conversion: Exploring std::from\_chars in C++17 to C++26

DevFeed: [C++ String Conversion: Exploring std::from\_chars in C++17 to C++26](<https://devfeed.tech/articles/c-string-conversion-exploring-std-from-chars-in-c-17-to-c-26-53297.md>)

Original publisher: [Read original article](<https://www.cppstories.com/2018/12/fromchars/>)

Published: 2024-10-13T00:00:00Z

Content type: tutorial

Language: en

Sources: [C++ Stories](<https://devfeed.tech/sources/c-stories.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [API](<https://devfeed.tech/topics/api.md>), [Error Handling](<https://devfeed.tech/topics/error-handling.md>), [standard](<https://devfeed.tech/topics/standard.md>), [Memory Safety](<https://devfeed.tech/topics/memory-safety.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [c-plus-plus-23](<https://devfeed.tech/tags/c-plus-plus-23.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [error-handling](<https://devfeed.tech/tags/error-handling.md>), [error-reporting](<https://devfeed.tech/tags/error-reporting.md>), [memory-safety](<https://devfeed.tech/tags/memory-safety.md>), [performance](<https://devfeed.tech/tags/performance.md>), [refactoring](<https://devfeed.tech/tags/refactoring.md>)

## AI overview

A tutorial on std::from_chars, the C++17 Standard Library API for converting character sequences to numeric values. It compares the low-level, non-throwing and non-allocating API with older conversion methods, and discusses constexpr support, performance, and error handling through C++26.

## Source excerpt

With the introduction of C++17, the C++ Standard Library expanded its capabilities for converting text to numbers with the addition of std::from_chars. This low-level, high-performance API offers significant advantages over previous methods, such as atoi and stringstream. In this article, we will explore the evolution of string conversion routines from C++17 through C++26, highlighting key improvements like constexpr support and enhanced error handling. Let's dive into the details and see how std::from_chars can transform your approach to string conversion. Updated in Oct 2024: Added notes from constexpr support in C++23, and C++26 improvements, plus more Compiler Explorer examples. Before C++17 C++, before C++17, offered several options when it comes to string conversion: sprintf / snprintf sscanf atol strtol strstream stringstream to_string stoi and similar functions And with C++17 you get another option: std::from_chars! Wasn't the old stuff good enough? Why do we need new methods? In short: because from_chars is low-level, and offers the best possible performance. The new conversion routines are: non-throwing non-allocating no locale support memory safety error reporting gives additional information about the conversion outcome The API might not be the most friendly to use, but it's easy enough to wrap it into some facade. A simple example: const std::string str { "12345678901234" }; int value = 0; std::from_chars(str.data(),str.data() + str.size(), value); // error checking ommited... The Series This article is part of my series about C++17 Library Utilities. Here's the list of the topics in the series: Refactoring with std::optional Using std::optional Error handling and std::optional Everything You Need to Know About std::variant from C++17 Everything You Need to Know About std::any from C++17 std::string_view Performance and followup C++17 string searchers and followup Conversion utilities: on std::from_chars - from a string to a number and on std::to_chars