15 static uint8_t hex_char_to_int(
char c)
36 template <
typename Iter>
37 inline static std::string to_hex(Iter begin, Iter end)
39 return fmt::format(
"{:02x}", fmt::join(begin, end,
""));
43 inline static std::string to_hex(
const T& data)
45 return to_hex(data.begin(), data.end());
48 inline static std::string to_hex(std::span<const uint8_t> buf)
53 r += fmt::format(
"{:02x}", c);
60 template <
typename Iter>
61 static void from_hex(
const std::string& str, Iter begin, Iter end)
63 if ((str.size() & 1) != 0)
65 throw std::logic_error(fmt::format(
66 "Input string '{}' is not of even length: {}", str, str.size()));
69 if (std::distance(begin, end) != str.size() / 2)
71 throw std::logic_error(fmt::format(
72 "Output container of size {} cannot fit decoded hex str {}",
73 std::distance(begin, end),
78 for (
size_t i = 0; i < str.size(); i += 2, ++it)
80 *it =
hex_base * hex_char_to_int(str[i]) + hex_char_to_int(str[i + 1]);
84 inline static std::vector<uint8_t> from_hex(
const std::string& str)
86 std::vector<uint8_t> ret(str.size() / 2);
87 from_hex(str, ret.begin(), ret.end());
92 inline static void from_hex(
const std::string& str, T& out)
94 from_hex(str, out.begin(), out.end());
Definition contiguous_set.h:12
constexpr size_t hex_base
Definition hex.h:58
constexpr size_t ascii_offset
Definition hex.h:14