14 static uint8_t hex_char_to_int(
char c)
32 template <
typename Iter>
33 inline static std::string to_hex(Iter begin, Iter end)
35 return fmt::format(
"{:02x}", fmt::join(begin, end,
""));
39 inline static std::string to_hex(
const T& data)
41 return to_hex(data.begin(), data.end());
44 inline static std::string to_hex(std::span<const uint8_t> buf)
48 r += fmt::format(
"{:02x}", c);
52 template <
typename Iter>
53 static void from_hex(
const std::string& str, Iter begin, Iter end)
55 if ((str.size() & 1) != 0)
57 throw std::logic_error(fmt::format(
58 "Input string '{}' is not of even length: {}", str, str.size()));
61 if (std::distance(begin, end) != str.size() / 2)
63 throw std::logic_error(fmt::format(
64 "Output container of size {} cannot fit decoded hex str {}",
65 std::distance(begin, end),
70 for (
size_t i = 0; i < str.size(); i += 2, ++it)
72 *it = 16 * hex_char_to_int(str[i]) + hex_char_to_int(str[i + 1]);
76 inline static std::vector<uint8_t> from_hex(
const std::string& str)
78 std::vector<uint8_t> ret(str.size() / 2);
79 from_hex(str, ret.begin(), ret.end());
84 inline static void from_hex(
const std::string& str, T& out)
86 from_hex(str, out.begin(), out.end());
Definition contiguous_set.h:12