2 * Matt McCutchen's Big Integer Library
6 * Milan Tomic had trouble compiling this file on Microsoft
7 * Visual C++ 6 because, in the libraries that come with
8 * Visual C++ 6, the `std::string::push_back' method apparently
9 * does not exist. To get around the problem, I rewrote
10 * `BigUnsignedInABase::operator std::string' (at the bottom
11 * of this file) so it doesn't use `push_back'.
14 #include "BigUnsignedInABase.hh"
17 unsigned int bitLen(unsigned int x) {
25 unsigned int ceilingDiv(unsigned int a, unsigned int b) {
26 return (a + b - 1) / b;
30 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() * BigUnsigned::N;
41 int minBitsPerDigit = bitLen(base) - 1;
42 int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit);
43 len = maxDigitLenOfX; // Another change to comply with `staying in bounds'; see `BigUnsigned::divideWithRemainder'.
44 allocate(len); // Get the space
46 BigUnsigned x2(x), buBase(base);
49 while (!x2.isZero()) {
50 // Get last digit. This is like `lastDigit = x2 % buBase, x2 /= buBase'.
51 BigUnsigned lastDigit(x2);
52 lastDigit.divideWithRemainder(buBase, x2);
54 blk[digitNum] = Digit(lastDigit); // invokes `BigUnsigned ==> unsigned short' converter
55 // Move on. We can't run out of room: we figured it out above.
59 // Save the actual length.
63 BigUnsignedInABase::operator BigUnsigned() const {
64 BigUnsigned ans(0), buBase(base), temp;
66 while (digitNum > 0) {
68 temp.multiply(ans, buBase);
69 ans.add(temp, BigUnsigned(blk[digitNum]));
74 BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) {
77 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.";
79 // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
82 // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index',
83 // also known as an `unsigned int'. Some compilers warn without this cast.
84 len = Index(s.length());
87 Index digitNum, symbolNumInString;
88 for (digitNum = 0; digitNum < len; digitNum++) {
89 symbolNumInString = len - 1 - digitNum;
90 char theSymbol = s[symbolNumInString];
91 if (theSymbol >= '0' && theSymbol <= '9')
92 blk[digitNum] = theSymbol - '0';
93 else if (theSymbol >= 'A' && theSymbol <= 'Z')
94 blk[digitNum] = theSymbol - 'A' + 10;
95 else if (theSymbol >= 'a' && theSymbol <= 'z')
96 blk[digitNum] = theSymbol - 'a' + 10;
98 throw "BigUnsignedInABase(std::string, Base): Bad symbol in input. Only 0-9, A-Z, a-z are accepted.";
103 BigUnsignedInABase::operator std::string() const {
105 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.";
107 return std::string("0");
108 char *s = new char[len + 1];
110 Index digitNum, symbolNumInString;
111 for (symbolNumInString = 0; symbolNumInString < len; symbolNumInString++) {
112 digitNum = len - 1 - symbolNumInString;
113 Digit theDigit = blk[digitNum];
115 s[symbolNumInString] = char('0' + theDigit);
117 s[symbolNumInString] = char('A' + theDigit - 10);
120 // 2006.05.03: This needs to be [] to match the allocation