// https://www.boost.org/doc/libs/1_92_0/libs/charconv/doc/html/charconv.html

#include <boost/charconv.hpp>

#include <cassert>
#include <cstring>
#include <iostream>
#include <system_error>

int main()
{
    // Integer round trip: value -> chars -> value
    const int value = -123456;
    char buffer[64] = {};

    auto to = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value);
    assert(to.ec == std::errc());
    *to.ptr = '\0';

    std::cout << "to_chars(" << value << ") = " << buffer << std::endl;
    assert(std::strcmp(buffer, "-123456") == 0);

    int parsed = 0;
    auto from = boost::charconv::from_chars(buffer, to.ptr, parsed);
    assert(from.ec == std::errc());
    assert(parsed == value);

    // Non-decimal bases are supported as well
    char hex[64] = {};
    auto to_hex = boost::charconv::to_chars(hex, hex + sizeof(hex), 255, 16);
    assert(to_hex.ec == std::errc());
    *to_hex.ptr = '\0';

    std::cout << "to_chars(255, base 16) = " << hex << std::endl;
    assert(std::strcmp(hex, "ff") == 0);

    return 0;
}
