* Convert input string into a hex string * * Each character in input is interpreted as an unsigned char and represented as a two-digit hex value [0-255] in the returned * string. * * @param input Input string view * @return The hex representation of the input string * * @note this is a specialization of boost::algorithms::hex */
| 38 | * @note this is a specialization of boost::algorithms::hex |
| 39 | */ |
| 40 | inline std::string |
| 41 | hex(const std::string_view input) |
| 42 | { |
| 43 | std::string result; |
| 44 | |
| 45 | result.resize(input.size() * 2); |
| 46 | |
| 47 | char *p = result.data(); |
| 48 | char *end = p + result.size(); |
| 49 | for (unsigned char x : input) { |
| 50 | if (x < 0x10) { |
| 51 | *p++ = '0'; |
| 52 | } |
| 53 | if (auto [ptr, err] = std::to_chars(p, end, x, 16); err == std::errc()) { |
| 54 | p = ptr; |
| 55 | } else { |
| 56 | throw std::runtime_error(std::make_error_code(err).message().c_str()); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Convert input hex string into a string |