| 103 | } |
| 104 | |
| 105 | bytes |
| 106 | from_hex(const std::string& hex) |
| 107 | { |
| 108 | if (hex.length() % 2 == 1) { |
| 109 | throw std::invalid_argument("Odd-length hex string"); |
| 110 | } |
| 111 | |
| 112 | auto len = hex.length() / 2; |
| 113 | auto out = bytes(len); |
| 114 | for (size_t i = 0; i < len; i += 1) { |
| 115 | const std::string byte = hex.substr(2 * i, 2); |
| 116 | out.at(i) = static_cast<uint8_t>(strtol(byte.c_str(), nullptr, 16)); |
| 117 | } |
| 118 | |
| 119 | return out; |
| 120 | } |
| 121 | |
| 122 | std::ostream& |
| 123 | operator<<(std::ostream& out, const bytes& data) |