* Convert input hex string into a string * * The input string must have even size and be comprised of hex digits [0-f]. Each two-digit pair is converted from hex to a * character in the resulting output string. * * @param input Input string view * @return The unhexified result string * * @note this is a specialization of boost::algorithms::unhex */
| 72 | * @note this is a specialization of boost::algorithms::unhex |
| 73 | */ |
| 74 | inline std::string |
| 75 | unhex(const std::string_view input) |
| 76 | { |
| 77 | std::string result; |
| 78 | |
| 79 | if (input.size() % 2 != 0) { |
| 80 | throw std::invalid_argument("input to unhex needs to be an even size"); |
| 81 | } |
| 82 | |
| 83 | result.resize(input.size() / 2); |
| 84 | |
| 85 | const char *p = input.data(); |
| 86 | for (auto &x : result) { |
| 87 | if (auto [ptr, err] = std::from_chars(p, p + 2, reinterpret_cast<unsigned char &>(x), 16); err == std::errc()) { |
| 88 | p = ptr; |
| 89 | } else { |
| 90 | throw std::runtime_error(std::make_error_code(err).message().c_str()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | } // namespace ts |