What Are The Elementary String Conversions That Come With C++ 17?
In addition to many beneficial features of C++ 17, there are elementary string conversions introduced in that specification. The std::to_chars()
and std::from_chars()
are defined in
header to do conversions between numeric values to strings or strings to numeric values without considering locale-specific conversions. In this post, we explain the std::to_chars()
and std::from_chars()
that come with C++17.
What Are The Elementary String Conversions That Come With C++ 17 ?
What is std::to_chars()?
The std::to_chars()
is defined in header and is used to convert numeric values into a character string within the given valid range.
The std::to_chars()
is designed to copy the numeric value into a string buffer, with a given specific format, with the only overhead of making sure that the buffer is big enough. You don’t need to consider locale-specific conversions.
Here is the syntax of std::to_chars
in C++ 17.
std::to_chars_result to_chars( char* first, char* last, value, int base = 10 );
|
Here is a simple example.
std::string str= “abcdefgh”; const int ival = 10001000; const auto con = std::to_chars( str.data(), str.data() + str.size(), ival);
|
In floating point number conversions, std::chars_format
types can be used (i.e. std::chars_format::fixed
, std::chars_format::scientific
, std::chars_format::general
,…)
What is std::from_chars()?
The std::from_chars()
is defined in the header and used to convert the string data in a given range to a value (string to int, string to float operations) if no string characters match the pattern or if the obtained value is not representable in a given type of value, then the value has remains unchanged.
The std::from_chars()
is a lightweight parser that does not need to create dynamic allocation, and you don’t need to consider locale-specific conversions.
Here is the syntax of std::from_chars
in C++ 17.
std::from_chars_result from_chars( const char* first, const char* last, &value, int base = 10 );
|
Here is a simple example.
std::string str= “10001000”; int vali; auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), vali);
|
Is there a full example to elementary string conversions that comes with C++ 17?
Here is a full example about std::to_chars()
and std::from_chars()
in C++ 17.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include #include #include
int main() { std::string str= “abcdefgh”;
// INT TO CHAR const int ival = 10001000; const auto con = std::to_chars( str.data(), str.data() + str.size(), ival);
std::cout << str << std::endl;
// FLOAT TO CHAR std::string str2= “abcdefgh”; const float fval = 9.81f; const auto con2 = std::to_chars( str2.data(), str2.data() + str2.size(), fval, std::chars_format::fixed, 6); std::cout << str2 << std::endl;
// CHAR TO INT int vali; auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), vali); std::cout << vali << std::endl;
// CHAR TO FLOAT float valf; auto [ptr2, ec2] = std::from_chars(str2.data(), str2.data() + str2.size(), valf); std::cout << valf << std::endl;
system(“pause”); return 0; }
|
Here is the output.
10001000 9.810000 10001000 9.81
|
Note that here ec
is error check parameter and can be used to check error by if( ec = std::erc() ) { ... }
or other error check members can be used, i.e. if (ec == std::errc::invalid_argument) { ... }
. The benefits of these conversion methods are compatible with the locale-specific conversions.
For more details about this feature in C++17 standard, please see these papers; P0067R5, P0682R1DR
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.