C++ charconv to_chars() Function



The std::to_chars() that converts numeric values into character sequences. It allows fast, locale-independent conversion of integral and floating-point types to their textual representation.

Unlike standard I/O-based functions like sprintf(), std::to_chars() does not require memory allocation or format parsing, making it an efficient option for performance-critical applications. The function writes the converted value into a user-provided character array and returns a pointer to the next character after the written number.

Syntax

Following is the syntax for std::from_chars() Function.

std::to_chars_result to_chars(char* first, char* last, Integral value);
std::to_chars_result to_chars(char* first, char* last, Floating value, std::chars_format fmt);

Parameters

  • first : Pointer to the beginning of the buffer where the character sequence will be stored.
  • last : Pointer to one past the end of the buffer.
  • value : The numeric value to be converted to a character sequence.
  • base (optional) : he numerical base (radix) to use for the conversion, which can range from 2 to 36. If not specified, the default is base 10.

Return Value

The function returns an std::to_chars_result structure which contains:

  • ptr : A pointer to one past the last character written.
  • ec : An error code indicating the result of the conversion.

Time Complexity

The time complexity of this function is constant, i.e.,O(1) for integral values and approximately O(N) for floating-point values, depending on the precision and formatting used.

Example 1

This example converts an integer to a string and stores it in buffer. The to_chars() function writes the result into the buffer, and the program prints the converted value.

#include <iostream>
#include <charconv>

int main() {
   char buffer[20];
   int value = 12345;
   auto result = std::to_chars(buffer, buffer + sizeof(buffer), value);
   
   if (result.ec == std::errc()) {
      std::cout << "Integer conversion: " << std::string(buffer, result.ptr) << std::endl;
   } else {
      std::cout << "Conversion failed!" << std::endl;
   }
   return 0;
}

Output

Output of the above code is as follows

Integer conversion: 12345

Example 2

This example converts a floating-point number to a string using std::chars_format::fixed, ensuring a fixed-point representation.

#include <iostream>
#include <charconv>
int main() {
   char buffer[20];
   double value = 123.456;
   auto result = std::to_chars(buffer, buffer + sizeof(buffer), value, std::chars_format::fixed);
   
   if (result.ec == std::errc()) {
      std::cout << "Floating-point conversion (fixed): " << std::string(buffer, result.ptr) << std::endl;
   } else {
      std::cout << "Conversion failed!" << std::endl;
   }
   return 0;
}

Output

If we run the above code it will generate the following output

Floating-point conversion (fixed): 123.456

Example 3

Here, the floating-point number is converted using std::chars_format::scientific, producing an output in scientific notation.

#include <iostream>
#include <charconv>

int main() {
   char buffer[20];
   int value = 12345;
   auto result = std::to_chars(buffer, buffer + sizeof(buffer), value);
   
   if (result.ec == std::errc()) {
      std::cout << "Integer conversion: " << std::string(buffer, result.ptr) << std::endl;
   } else {
      std::cout << "Conversion failed!" << std::endl;
   }
   return 0;
}

Output

Following is the output of the above code

Floating-point conversion (scientific): 1.23e-04
charconv.htm
Advertisements
close