Electroneum
epee::from_hex Struct Reference

#include <hex.h>

Static Public Member Functions

static std::vector< uint8_tvector (boost::string_ref src)
 

Detailed Description

Definition at line 74 of file hex.h.

Member Function Documentation

◆ vector()

std::vector< uint8_t > epee::from_hex::vector ( boost::string_ref  src)
static
Returns
An std::vector of unsigned integers from the src

Definition at line 88 of file hex.cpp.

89  {
90  // should we include a specific character
91  auto include = [](char input) {
92  // we ignore spaces and colons
93  return !std::isspace(input) && input != ':';
94  };
95 
96  // the number of relevant characters to decode
97  auto count = std::count_if(src.begin(), src.end(), include);
98 
99  // this must be a multiple of two, otherwise we have a truncated input
100  if (count % 2) {
101  throw std::length_error{ "Invalid hexadecimal input length" };
102  }
103 
104  std::vector<uint8_t> result;
105  result.reserve(count / 2);
106 
107  // the data to work with (std::string is always null-terminated)
108  auto data = src.data();
109 
110  // convert a single hex character to an unsigned integer
111  auto char_to_int = [](const char *input) {
112  switch (std::tolower(*input)) {
113  case '0': return 0;
114  case '1': return 1;
115  case '2': return 2;
116  case '3': return 3;
117  case '4': return 4;
118  case '5': return 5;
119  case '6': return 6;
120  case '7': return 7;
121  case '8': return 8;
122  case '9': return 9;
123  case 'a': return 10;
124  case 'b': return 11;
125  case 'c': return 12;
126  case 'd': return 13;
127  case 'e': return 14;
128  case 'f': return 15;
129  default: throw std::range_error{ "Invalid hexadecimal input" };
130  }
131  };
132 
133  // keep going until we reach the end
134  while (data[0] != '\0') {
135  // skip unwanted characters
136  if (!include(data[0])) {
137  ++data;
138  continue;
139  }
140 
141  // convert two matching characters to int
142  auto high = char_to_int(data++);
143  auto low = char_to_int(data++);
144 
145  result.push_back(high << 4 | low);
146  }
147 
148  return result;
149  }
mdb_size_t count(MDB_cursor *cur)
Here is the call graph for this function:

The documentation for this struct was generated from the following files: