13 static uint8_t hex_char_to_int(
char c)
31 template <
typename Iter>
32 inline static std::string to_hex(Iter begin, Iter end)
34 return fmt::format(
"{:02x}", fmt::join(begin, end,
""));
38 inline static std::string to_hex(
const T& data)
40 return to_hex(data.begin(), data.end());
43 inline static std::string to_hex(std::span<const uint8_t> buf)
47 r += fmt::format(
"{:02x}", c);
51 template <
typename Iter>
52 static void from_hex(
const std::string& str, Iter begin, Iter end)
54 if ((str.size() & 1) != 0)
56 throw std::logic_error(fmt::format(
57 "Input string '{}' is not of even length: {}", str, str.size()));
60 if (std::distance(begin, end) != str.size() / 2)
62 throw std::logic_error(fmt::format(
63 "Output container of size {} cannot fit decoded hex str {}",
64 std::distance(begin, end),
69 for (
size_t i = 0; i < str.size(); i += 2, ++it)
71 *it = 16 * hex_char_to_int(str[i]) + hex_char_to_int(str[i + 1]);
75 inline static std::vector<uint8_t> from_hex(
const std::string& str)
77 std::vector<uint8_t> ret(str.size() / 2);
78 from_hex(str, ret.begin(), ret.end());
83 inline static void from_hex(
const std::string& str, T& out)
85 from_hex(str, out.begin(), out.end());
Definition contiguous_set.h:11