7 enum class byte_encode { raw_bytes, hex_bytes, base58_bytes, base64_bytes };
10 static inline char dec2hex(uint8_t dec) {
12 throw std::invalid_argument(
"Invalid decimal");
14 if (dec >= 0 && dec <= 9) {
17 return 'a' + dec - 10;
20 static inline void to_hex(uint8_t num, uint8_t *high, uint8_t *low) {
21 *low = dec2hex(num & 0x0f);
22 *high = dec2hex(num >> 4);
25 static inline uint8_t hex2dec(uint8_t hex) {
26 if (hex >=
'0' && hex <=
'9') {
29 if (hex >=
'A' && hex <=
'F') {
30 return hex -
'A' + 10;
32 if (hex >=
'a' && hex <=
'f') {
33 return hex -
'a' + 10;
35 throw std::invalid_argument(
"Invalid hex");
37 template <
typename ByteType1,
typename ByteType2>
38 bool convert_hex_to_bytes(
const ByteType1 *s,
size_t s_size, ByteType2 *to,
40 static_assert(
sizeof(ByteType1) ==
sizeof(ByteType2));
41 static_assert(
sizeof(ByteType1) == 1);
42 if (len < s_size / 2) {
47 while (i * 2 < s_size && i * 2 + 1 < s_size) {
49 to[i] = (hex2dec(s[i * 2]) << 4) + hex2dec(s[i * 2 + 1]);
53 }
catch (std::exception &e) {
59 template <
typename ByteType1,
typename ByteType2>
60 bool convert_bytes_to_hex(
const ByteType1 *s,
size_t s_size, ByteType2 *to,
62 if (len < 2 * s_size) {
65 for (
size_t i = 0; i < s_size; i++) {
66 to_hex(s[i], to + 2 * i, to + 2 * i + 1);