DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples

Trending

  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • Memory Leak Due to Time-Taking finalize() Method
  • Infrastructure as Code (IaC) Beyond the Basics
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Use The Newest C++ String Conversion Routines: std::from_chars

How to Use The Newest C++ String Conversion Routines: std::from_chars

With C++17 we get another facility to handle the conversion between text and numbers. Why should we care about the new routines? Are they better in any way?

By 
Bartłomiej Filipek user avatar
Bartłomiej Filipek
·
Feb. 19, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
10.6K Views

Join the DZone community and get the full member experience.

Join For Free

This article was originally posted at the author's blog: bfilipek.com

Intro

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
  • have no locale support
  • memory safe
  • error reporting, which 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 new methods are available in the following compilers:

  • Visual Studio 2017 15.9 - full support (from_chars and also to_chars) (see notes about changes in 15.8 and in 15.9)
  • GCC - 8.0 - still in progress, only integer support
  • Clang 7.0 - still in progress, only integer support

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
  • Aboutstd::variant
  • Aboutstd::any
  • In place construction for std::optional, std::variant and std::any
  • std::string_view Performance and follow-up and another post about string initialization
  • C++17 string searchers and follow-up
  • Conversion utilities (this post)
  • Working with std::filesystem

Resources about C++17 STL:

  • C++17 In Detail by Bartek!
  • C++17 - The Complete Guide by Nicolai Josuttis
  • C++ Fundamentals Including C++ 17 by Kate Gregory
  • Practical C++14 and C++17 Features - by Giovanni Dicanio
  • C++17 STL Cookbook by Jacek Galowicz

Let’s have a look at the API now.

Converting From Characters to Numbers: from_chars

std::from_chars is a set of overloaded functions: for integral types and floating point types.

For integral types we have the following functions:

std::from_chars_result from_chars(const char* first, 
                                  const char* last, 
                                  TYPE &value,
                                  int base = 10);

Where TYPE expands to all available signed and unsigned integer types and char.

base can be a number ranging from 2 to 36.

Then there’s the floating point version:

std::from_chars_result from_chars(const char* first, 
                   const char* last, 
                   FLOAT_TYPE& value,
                   std::chars_format fmt = std::chars_format::general);

FLOAT_TYPE expands to float, double or long double.

chars_format is an enum with the following values: scientific, fixed, hex and general (which is a composition of fixed and scientific).

The return value in all of those functions (for integers and floats) is from_chars_result:

struct from_chars_result {
    const char* ptr;
    std::errc ec;
};

from_chars_result holds valuable information about the conversion process.

Here’s the summary:

Return Condition State of from_chars_result
Success ptr points at the first character not matching the pattern, or has the value equal to last if all characters match and ec is value-initialized.
Invalid conversion ptr equals first and ec equals std::errc::invalid_argument. value is unmodified.
Out of range The number it too large to fit into the value type. ec equals std::errc::result_out_of_range and ptr points at the first character not matching the pattern. value is unmodified.

Examples

Here are two examples of how to convert a string into a number using from_chars, to int andfloat.

Integral Types

#include <charconv> // from_char, to_char
#include <string>
#include <iostream>

int main()
{
    const std::string str { "12345678901234" };
    int value = 0;
    const auto res = std::from_chars(str.data(), 
                                     str.data() + str.size(), 
                                     value);

    if (res.ec == std::errc())
    {
        std::cout << "value: " << value 
                  << ", distance: " << res.ptr - str.data() << '\n';
    }
    else if (res.ec == std::errc::invalid_argument)
    {
        std::cout << "invalid argument!\n";
    }
    else if (res.ec == std::errc::result_out_of_range)
    {
        std::cout << "out of range! res.ptr distance: " 
                  << res.ptr - str.data() << '\n';
    }
}

The example is straightforward, it passes a string str into from_chars and then displays the result with additional information if possible.

Run the code below and change the str value to see the output:

Floating Point

To get the floating point test, we can replace the top lines of the previous example with:

// works with MSVC only so far (Dec 2018)
const std::string str { "16.78" };
double value = 0;
const auto format = std::chars_format::general;
const auto res = std::from_chars(str.data(), 
                                 str.data() + str.size(), 
                                 value, 
                                 format);

Here’s the example output that we can get:

str value format value output
1.01 fixed value: 1.01, distance 4
-67.90000 fixed value: -67.9, distance: 9
20.9 scientific invalid argument!, res.p distance: 0
20.9e+0 scientific value: 20.9, distance: 7
-20.9e+1 scientific value: -209, distance: 8
F.F hex value: 15.9375, distance: 3
-10.1 hex value: -16.0625, distance: 5

The general format is a combination of fixed and scientific so it handles regular floating point string with the additional support for e+num syntax.

A Bit About Performance

I did some benchmarking and the new routines are blazing fast!

Some numbers:

  • On GCC it’s ~4.5x faster than stoi, 2.2x faster than atoi and almost 50x faster than istringstream.
  • On Clang it’s ~3.5x faster than stoi, 2.7x faster than atoi and 60x faster than istringstream!
  • MSVC performs ~3x faster than stoi, ~2x faster than atoi and almost 50x faster than istringstream

The whole benchmark might, however, be a story for another post, so I’m not sharing the code yet.

Summary

If you want to convert text into a number and you don’t need any extra stuff like locale support then std::from_chars might be the best choice. It offers great performance, and what’s more, you’ll get a lot of information about the conversion process (for example, how many characters were scanned).

The routines might be especially handy with parsing JSON files, 3D textual model representation (like OBJ file formats), etc.

More from the Author:

Image title

Bartek recently published a book - "C++17 In Detail"- learn the new C++ Standard in an efficient and practical way. The book contains more than 300 pages filled with C++17 content!

Your Turn

Have you played with the new conversion routines? What do you usually use to convert text into numbers?

Data Types Strings c++

Published at DZone with permission of Bartłomiej Filipek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: