2 * Matt McCutchen's Big Integer Library
3 * http://mysite.verizon.net/mccutchen/bigint/
7 * Milan Tomic had trouble compiling this file on Microsoft
8 * Visual C++ 6 because, in the libraries that come with
9 * Visual C++ 6, the `std::string::push_back' method apparently
10 * does not exist. To get around the problem, I rewrote
11 * `BigUnsignedInABase::operator std::string' (at the bottom
12 * of this file) so it doesn't use `push_back'.
15 #include "BigUnsignedInABase.hh"
18 unsigned int bitLen(unsigned int x) {
26 unsigned int ceilingDiv(unsigned int a, unsigned int b) {
27 return (a + b - 1) / b;
31 BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
34 throw "BigUnsignedInABase(BigUnsigned, Base): The base must be at least 2";
36 // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
39 // Get an upper bound on how much space we need
40 int maxBitLenOfX = x.getLength() * 8 * sizeof(BigUnsigned::Blk);
41 int minBitsPerDigit = bitLen(base) - 1;
42 int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit);
43 allocate(maxDigitLenOfX); // Get the space
45 BigUnsigned x2(x), buBase(base);
48 while (!x2.isZero()) {
49 // Get last digit. This is like `lastDigit = x2 % buBase, x2 /= buBase'.
50 BigUnsigned lastDigit(x2);
51 lastDigit.divideWithRemainder(buBase, x2);
53 blk[digitNum] = Digit(lastDigit); // invokes `BigUnsigned ==> unsigned short' converter
54 // Move on. We can't run out of room: we figured it out above.
58 // Save the eventual length.
62 BigUnsignedInABase::operator BigUnsigned() const {
63 BigUnsigned ans(0), buBase(base), temp;
65 while (digitNum > 0) {
67 temp.multiply(ans, buBase);
68 ans.add(temp, BigUnsigned(blk[digitNum]));
73 BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) {
76 throw "BigUnsignedInABase(std::string, Base): The default string conversion routines use the symbol set 0-9, A-Z and therefore support only up to base 36. You tried a conversion with a base over 36; write your own string conversion routine.";
78 // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
84 Index digitNum, symbolNumInString;
85 for (digitNum = 0; digitNum < len; digitNum++) {
86 symbolNumInString = len - 1 - digitNum;
87 char theSymbol = s[symbolNumInString];
88 if (theSymbol >= '0' && theSymbol <= '9')
89 blk[digitNum] = theSymbol - '0';
90 else if (theSymbol >= 'A' && theSymbol <= 'Z')
91 blk[digitNum] = theSymbol - 'A' + 10;
92 else if (theSymbol >= 'a' && theSymbol <= 'z')
93 blk[digitNum] = theSymbol - 'a' + 10;
95 throw "BigUnsignedInABase(std::string, Base): Bad symbol in input. Only 0-9, A-Z, a-z are accepted.";
100 BigUnsignedInABase::operator std::string() const {
102 throw "BigUnsignedInABase ==> std::string: The default string conversion routines use the symbol set 0-9, A-Z and therefore support only up to base 36. You tried a conversion with a base over 36; write your own string conversion routine.";
104 return std::string("0");
105 char *s = new char[len + 1];
107 Index digitNum, symbolNumInString;
108 for (symbolNumInString = 0; symbolNumInString < len; symbolNumInString++) {
109 digitNum = len - 1 - symbolNumInString;
110 Digit theDigit = blk[digitNum];
112 s[symbolNumInString] = char('0' + theDigit);
114 s[symbolNumInString] = char('A' + theDigit - 10);
116 return std::string(s);