| #include "common.h" |
| |
| #include <iomanip> |
| #include <sstream> |
| |
| bytes |
| from_hex(const std::string& hex) |
| { |
| if (hex.length() % 2 == 1) { |
| throw std::invalid_argument("Odd-length hex string"); |
| } |
| |
| auto len = int(hex.length() / 2); |
| auto out = bytes(len); |
| for (int i = 0; i < len; i += 1) { |
| auto byte = hex.substr(2 * i, 2); |
| out[i] = static_cast<uint8_t>(strtol(byte.c_str(), nullptr, 16)); |
| } |
| |
| return out; |
| } |
| |
| std::string |
| to_hex(const sframe::input_bytes data) |
| { |
| std::stringstream hex(std::ios_base::out); |
| hex.flags(std::ios::hex); |
| for (const auto& byte : data) { |
| hex << std::setw(2) << std::setfill('0') << int(byte); |
| } |
| return hex.str(); |
| } |