Public Member Functions | Static Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
cet::base_converter Class Reference

#include <base_converter.h>

Public Member Functions

std::string const & get_source_base_set () const
 
std::string const & get_target_base_set () const
 
unsigned int get_source_base () const
 
unsigned int get_target_base () const
 
 base_converter (std::string const &sourceBaseSet, std::string const &targetBaseSet)
 
std::string convert (std::string value) const
 

Static Public Member Functions

static std::string dec_to_bin (std::string value)
 
static std::string bin_to_dec (std::string value)
 
static std::string dec_to_hex (std::string value)
 
static std::string hex_to_dec (std::string value)
 

Static Private Member Functions

static unsigned int divide (std::string const &baseDigits, std::string &x, unsigned int y)
 
static unsigned int base2dec (std::string const &baseDigits, std::string const &value)
 
static std::string dec2base (std::string const &baseDigits, unsigned int value)
 
static constexpr char const * binary_set ()
 
static constexpr char const * decimal_set ()
 
static constexpr char const * hex_set ()
 

Private Attributes

std::string source_base_set_
 
std::string target_base_set_
 

Detailed Description

Definition at line 18 of file base_converter.h.

Constructor & Destructor Documentation

base_converter::base_converter ( std::string const &  sourceBaseSet,
std::string const &  targetBaseSet 
)

Definition at line 7 of file base_converter.cc.

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 }
std::string source_base_set_
std::string target_base_set_

Member Function Documentation

unsigned int base_converter::base2dec ( std::string const &  baseDigits,
std::string const &  value 
)
staticprivate

Definition at line 107 of file base_converter.cc.

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 }
static QCString result
std::string base_converter::bin_to_dec ( std::string  value)
static

Definition at line 23 of file base_converter.cc.

24 {
25  static const base_converter bin2dec{binary_set(), decimal_set()};
26  return bin2dec.convert(value);
27 }
static constexpr char const * binary_set()
static constexpr char const * decimal_set()
bool bin2dec(std::string const &input, std::string const &wanted)
static constexpr char const* cet::base_converter::binary_set ( )
inlinestaticprivate

Definition at line 48 of file base_converter.h.

49  {
50  return "01";
51  }
std::string base_converter::convert ( std::string  value) const

Definition at line 44 of file base_converter.cc.

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 }
static QCString result
std::string string
Definition: nybbler.cc:12
static unsigned int divide(std::string const &baseDigits, std::string &x, unsigned int y)
std::string source_base_set_
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_
std::string base_converter::dec2base ( std::string const &  baseDigits,
unsigned int  value 
)
staticprivate

Definition at line 93 of file base_converter.cc.

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 }
static QCString result
std::string string
Definition: nybbler.cc:12
static unsigned int reverse(QString &chars, unsigned char *level, unsigned int a, unsigned int b)
Definition: qstring.cpp:11649
std::string base_converter::dec_to_bin ( std::string  value)
static

Definition at line 16 of file base_converter.cc.

17 {
18  static const base_converter dec2bin{decimal_set(), binary_set()};
19  return dec2bin.convert(value);
20 }
static constexpr char const * binary_set()
static constexpr char const * decimal_set()
bool dec2bin(std::string const &input, std::string const &wanted)
std::string base_converter::dec_to_hex ( std::string  value)
static

Definition at line 30 of file base_converter.cc.

31 {
32  static const base_converter dec2hex{decimal_set(), hex_set()};
33  return dec2hex.convert(value);
34 }
bool dec2hex(std::string const &input, std::string const &wanted)
static constexpr char const * decimal_set()
static constexpr char const * hex_set()
static constexpr char const* cet::base_converter::decimal_set ( )
inlinestaticprivate

Definition at line 53 of file base_converter.h.

54  {
55  return "0123456789";
56  }
unsigned int base_converter::divide ( std::string const &  baseDigits,
std::string x,
unsigned int  y 
)
staticprivate

Definition at line 60 of file base_converter.cc.

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 }
std::string string
Definition: nybbler.cc:12
static unsigned int base2dec(std::string const &baseDigits, std::string const &value)
static std::string dec2base(std::string const &baseDigits, unsigned int value)
std::void_t< T > n
list x
Definition: train.py:276
unsigned int cet::base_converter::get_source_base ( ) const
inline

Definition at line 80 of file base_converter.h.

81 {
82  return (unsigned int)source_base_set_.length();
83 }
std::string source_base_set_
std::string const & cet::base_converter::get_source_base_set ( ) const
inline

Definition at line 68 of file base_converter.h.

69 {
70  return source_base_set_;
71 }
std::string source_base_set_
unsigned int cet::base_converter::get_target_base ( ) const
inline

Definition at line 86 of file base_converter.h.

87 {
88  return (unsigned int)target_base_set_.length();
89 }
std::string target_base_set_
std::string const & cet::base_converter::get_target_base_set ( ) const
inline

Definition at line 74 of file base_converter.h.

75 {
76  return target_base_set_;
77 }
std::string target_base_set_
static constexpr char const* cet::base_converter::hex_set ( )
inlinestaticprivate

Definition at line 58 of file base_converter.h.

59  {
60  return "0123456789ABCDEF";
61  }
std::string base_converter::hex_to_dec ( std::string  value)
static

Definition at line 37 of file base_converter.cc.

38 {
39  static const base_converter hex2dec{hex_set(), decimal_set()};
40  return hex2dec.convert(value);
41 }
static constexpr char const * decimal_set()
static constexpr char const * hex_set()
bool hex2dec(std::string const &input, std::string const &wanted)

Member Data Documentation

std::string cet::base_converter::source_base_set_
private

Definition at line 63 of file base_converter.h.

std::string cet::base_converter::target_base_set_
private

Definition at line 64 of file base_converter.h.


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