<charconv> Support
Description
The library provides character conversion functions for safe integer types using Boost.Charconv. These functions convert between safe integer types and their string representations.
#include <boost/safe_numbers/charconv.hpp>
namespace boost::safe_numbers {
// Convert safe integer to character string
template <unsigned_integral BasisType>
constexpr auto to_chars(char* first, char* last,
unsigned_integer_basis<BasisType> value,
int base = 10) -> charconv::to_chars_result;
// Convert character string to safe integer
template <unsigned_integral BasisType>
constexpr auto from_chars(const char* first, const char* last,
unsigned_integer_basis<BasisType>& value,
int base = 10) -> charconv::from_chars_result;
} // namespace boost::safe_numbers
to_chars_result
namespace boost::charconv {
struct to_chars_result
{
char* ptr;
std::errc ec;
friend constexpr bool operator==(const to_chars_result& lhs,
const to_chars_result& rhs) noexcept = default;
constexpr explicit operator bool() const noexcept { return ec == std::errc{}; }
};
} // namespace boost::charconv
| Member | Description |
|---|---|
|
Pointer to one past the last character written on success, or |
|
Error code: |
from_chars_result
namespace boost::charconv {
struct from_chars_result
{
const char* ptr;
std::errc ec;
friend constexpr bool operator==(const from_chars_result& lhs,
const from_chars_result& rhs) noexcept = default;
constexpr explicit operator bool() const noexcept { return ec == std::errc{}; }
};
} // namespace boost::charconv
| Member | Description |
|---|---|
|
Pointer to the first character not matching the pattern, or |
|
Error code: |
to_chars
template <unsigned_integral BasisType>
constexpr auto to_chars(char* first, char* last,
unsigned_integer_basis<BasisType> value,
int base = 10) -> charconv::to_chars_result;
Converts a safe integer value into a character buffer specified by [first, last).
from_chars
template <unsigned_integral BasisType>
constexpr auto from_chars(const char* first, const char* last,
unsigned_integer_basis<BasisType>& value,
int base = 10) -> charconv::from_chars_result;
Parses a string from [first, last) and converts it into a safe integer value.
Examples
// Copyright 2026 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/safe_numbers/unsigned_integers.hpp>
#include <boost/safe_numbers/charconv.hpp>
#include <iostream>
#include <cstring>
int main()
{
using namespace boost::safe_numbers;
// to_chars: Convert safe integer to string
u32 value {12345};
char buffer[32];
auto result = to_chars(buffer, buffer + sizeof(buffer), value);
if (result)
{
*result.ptr = '\0'; // Null-terminate
std::cout << "to_chars (base 10): " << buffer << '\n';
}
// to_chars with different bases
result = to_chars(buffer, buffer + sizeof(buffer), value, 16);
if (result)
{
*result.ptr = '\0';
std::cout << "to_chars (base 16): " << buffer << '\n';
}
result = to_chars(buffer, buffer + sizeof(buffer), value, 2);
if (result)
{
*result.ptr = '\0';
std::cout << "to_chars (base 2): " << buffer << '\n';
}
std::cout << '\n';
// from_chars: Convert string to safe integer
const char* str = "98765";
u32 parsed_value {};
auto parse_result = from_chars(str, str + std::strlen(str), parsed_value);
if (parse_result)
{
std::cout << "from_chars (base 10): " << static_cast<std::uint32_t>(parsed_value) << '\n';
}
// from_chars with hexadecimal
const char* hex_str = "1a2b";
u32 hex_value {};
parse_result = from_chars(hex_str, hex_str + std::strlen(hex_str), hex_value, 16);
if (parse_result)
{
std::cout << "from_chars (base 16): " << static_cast<std::uint32_t>(hex_value) << '\n';
}
// from_chars with binary
const char* bin_str = "11010";
u8 bin_value {};
parse_result = from_chars(bin_str, bin_str + std::strlen(bin_str), bin_value, 2);
if (parse_result)
{
std::cout << "from_chars (base 2): " << static_cast<unsigned>(bin_value) << '\n';
}
return 0;
}
Output:
to_chars (base 10): 12345 to_chars (base 16): 3039 to_chars (base 2): 11000000111001 from_chars (base 10): 98765 from_chars (base 16): 6699 from_chars (base 2): 26