DiceKeys Seeded Cryptography Library
convert.hpp
1 #pragma once
2 
3 #include <stdexcept>
4 #include <string>
5 #include <vector>
6 #include <algorithm>
7 #include "sodium-buffer.hpp"
8 
9 
10 class InvalidHexCharacterException : public std::invalid_argument
11 {
12  public:
13  explicit InvalidHexCharacterException(const char* what =
14  "Could not parse non-hex character"
15  ) :
16  std::invalid_argument(what) {}
17 };
18 
19 inline unsigned char parseHexChar(char c) {
20  if (c >= '0' && c <= '9') {
21  return c - '0';
22  } else if (c >= 'a' && c <= 'f') {
23  return 10 + (c - 'a');
24  } else if (c >= 'A' && c <= 'F') {
25  return 10 + (c - 'A');
26  }
27  throw InvalidHexCharacterException();
28 }
29 
30 const std::string toHexStr(const std::vector<unsigned char> bytes);
31 const std::vector<unsigned char> hexStrToByteVector(const std::string hexStr);
32 
33 inline std::string toUpper(const std::string& a) {
34  std::string upper = a;
35  std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
36  return upper;
37 }