base_converter.cc
Go to the documentation of this file.
1 #include "base_converter.h"
2 #include <algorithm>
3 #include <stdexcept>
4 
5 using namespace cet;
6 
8  std::string const& targetBaseSet)
9  : source_base_set_{sourceBaseSet}, target_base_set_{targetBaseSet}
10 {
11  if (sourceBaseSet.empty() || targetBaseSet.empty())
12  throw std::invalid_argument("Invalid base character set");
13 }
14 
17 {
18  static const base_converter dec2bin{decimal_set(), binary_set()};
19  return dec2bin.convert(value);
20 }
21 
24 {
25  static const base_converter bin2dec{binary_set(), decimal_set()};
26  return bin2dec.convert(value);
27 }
28 
31 {
32  static const base_converter dec2hex{decimal_set(), hex_set()};
33  return dec2hex.convert(value);
34 }
35 
38 {
39  static const base_converter hex2dec{hex_set(), decimal_set()};
40  return hex2dec.convert(value);
41 }
42 
45 {
46  unsigned int numberBase = get_target_base();
48 
49  do {
50  unsigned int remainder = divide(source_base_set_, value, numberBase);
51  result.push_back(target_base_set_[remainder]);
52  } while (!value.empty() &&
53  !(value.length() == 1 && value[0] == source_base_set_[0]));
54 
55  std::reverse(result.begin(), result.end());
56  return result;
57 }
58 
59 unsigned int
61  std::string& x,
62  unsigned int const y)
63 {
64  std::string quotient;
65 
66  size_t length = x.length();
67  for (size_t i = 0; i < length; ++i) {
68  size_t j = i + 1 + x.length() - length;
69  if (x.length() < j)
70  break;
71 
72  unsigned int value = base2dec(baseDigits, x.substr(0, j));
73 
74  quotient.push_back(baseDigits[value / y]);
75  x = dec2base(baseDigits, value % y) + x.substr(j);
76  }
77 
78  // calculate remainder
79  unsigned int remainder = base2dec(baseDigits, x);
80 
81  // remove leading "zeros" from quotient and store in 'x'
82  size_t n = quotient.find_first_not_of(baseDigits[0]);
83  if (n != std::string::npos) {
84  x = quotient.substr(n);
85  } else {
86  x.clear();
87  }
88 
89  return remainder;
90 }
91 
93 base_converter::dec2base(std::string const& baseDigits, unsigned int value)
94 {
95  unsigned int numberBase = (unsigned int)baseDigits.length();
97  do {
98  result.push_back(baseDigits[value % numberBase]);
99  value /= numberBase;
100  } while (value > 0);
101 
102  std::reverse(result.begin(), result.end());
103  return result;
104 }
105 
106 unsigned int
108  std::string const& value)
109 {
110  unsigned int numberBase = (unsigned int)baseDigits.length();
111  unsigned int result = 0;
112  for (size_t i = 0; i < value.length(); ++i) {
113  result *= numberBase;
114  size_t c = baseDigits.find(value[i]);
115  if (c == std::string::npos)
116  throw std::runtime_error("base_converter::Invalid character");
117 
118  result += (unsigned int)c;
119  }
120 
121  return result;
122 }
bool dec2hex(std::string const &input, std::string const &wanted)
static QCString result
static constexpr char const * binary_set()
std::string string
Definition: nybbler.cc:12
static unsigned int base2dec(std::string const &baseDigits, std::string const &value)
static std::string dec_to_hex(std::string value)
base_converter(std::string const &sourceBaseSet, std::string const &targetBaseSet)
std::string convert(std::string value) const
static constexpr char const * decimal_set()
static std::string dec2base(std::string const &baseDigits, unsigned int value)
std::void_t< T > n
static unsigned int divide(std::string const &baseDigits, std::string &x, unsigned int y)
static std::string dec_to_bin(std::string value)
bool dec2bin(std::string const &input, std::string const &wanted)
static constexpr char const * hex_set()
bool hex2dec(std::string const &input, std::string const &wanted)
std::string source_base_set_
static std::string hex_to_dec(std::string value)
static unsigned int reverse(QString &chars, unsigned char *level, unsigned int a, unsigned int b)
Definition: qstring.cpp:11649
unsigned int get_target_base() const
std::string target_base_set_
bool bin2dec(std::string const &input, std::string const &wanted)
list x
Definition: train.py:276
static std::string bin_to_dec(std::string value)