- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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