numeral system convert (partly)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Author: Alex A. Smirnov
TimeStamp: "2007-11-04 18:02 sceptik"
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <cmath>
namespace Convert{
namespace Utils{
std::string ToString(double value)
{
std::stringstream ssStream;
ssStream << value;
return ssStream.str();
}
int fromBinDecDigit (const std::string &number)
{
if (number == "0000") return 0;
if (number == "0001") return 1;
if (number == "0010") return 2;
if (number == "0011") return 3;
if (number == "0100") return 4;
if (number == "0101") return 5;
if (number == "0110") return 6;
if (number == "0111") return 7;
if (number == "1000") return 8;
if (number == "1001") return 9;
}
std::string toBinDecDigit (char number)
{
switch (number) {
case '0': return "0000";
case '1': return "0001";
case '2': return "0010";
case '3': return "0011";
case '4': return "0100";
case '5': return "0101";
case '6': return "0110";
case '7': return "0111";
case '8': return "1000";
case '9': return "1001";
case '.': return ".";
}
}
unsigned short digitToInt(char number)
{
if (number >= '0' && number <= '9')
return number - '0';
if (number >= 'A' && number <= 'Z')
return number - 'A' + 10;
return 0;
}
char intToDigit(unsigned short number)
{
if (number >= 0 && number <= 9)
return '0' + number;
if (number >= 10 && number <= 35)
return 'A' + number - 10;
return 0;
}
}
double decimalFromBinDec(const std::string &number)
{
}
std::string decimalToBinDec(double number)
{
std::string tmp = Utils::ToString(number), result;
for(std::string::iterator iter = tmp.begin(); iter != tmp.end(); ++iter)
result.append( Utils::toBinDecDigit(*iter) );
return result;
}
double anyToDecimal(const std::string &number, unsigned short radix)
{
double result = 0.0;
int pow = number.find('.');
if (pow < 0) pow = 0;
else pow -= number.length() - 1;
for(std::string::const_reverse_iterator criter = number.rbegin();
criter != number.rend(); ++criter) {
if (*criter != '.')
result += Utils::digitToInt(*criter) * std::powf(radix, pow++);
}
return result;
}
std::string decimalToAny(double number, unsigned short radix, int accuracy=5)
{
int wholep = int(number);
double fracp = number - wholep;
std::string result;
if (wholep) {
for (; wholep != 0; wholep /= radix)
result.push_back(Utils::intToDigit(wholep % radix));
std::reverse(result.begin(), result.end());
} else result.push_back('0');
if (fracp) {
result.push_back('.');
while (accuracy-- && fracp) {
fracp *= radix;
wholep = int(fracp);
fracp -= wholep;
result.push_back( Utils::intToDigit(wholep) );
}
}
return result;
}
}