2 * Milan Tomic had trouble compiling this file on Microsoft
3 * Visual C++ 6 because, in the libraries that come with
4 * Visual C++ 6, the `std::string::push_back' method apparently
5 * does not exist. To get around the problem, I rewrote
6 * `BigUnsignedInABase::operator std::string' (at the bottom
7 * of this file) so it doesn't use `push_back'.
10 #include "BigUnsignedInABase.hh"
13 unsigned int bitLen(unsigned int x) {
21 unsigned int ceilingDiv(unsigned int a, unsigned int b) {
22 return (a + b - 1) / b;
26 BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
30 throw "BigUnsignedInABase(BigUnsigned, Base): The base must be at least 2";
32 // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
35 // Get an upper bound on how much space we need
36 int maxBitLenOfX = x.getLength() * BigUnsigned::N;
37 int minBitsPerDigit = bitLen(base) - 1;
38 int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit);
39 len = maxDigitLenOfX; // Another change to comply with `staying in bounds'.
40 allocate(len); // Get the space
42 BigUnsigned x2(x), buBase(base);
45 while (!x2.isZero()) {
46 // Get last digit. This is like `lastDigit = x2 % buBase, x2 /= buBase'.
47 BigUnsigned lastDigit(x2);
48 lastDigit.divideWithRemainder(buBase, x2);
50 blk[digitNum] = lastDigit.toUnsignedShort();
51 // Move on. We can't run out of room: we figured it out above.
55 // Save the actual length.
59 BigUnsignedInABase::operator BigUnsigned() const {
60 BigUnsigned ans(0), buBase(base), temp;
62 while (digitNum > 0) {
64 temp.multiply(ans, buBase);
65 ans.add(temp, BigUnsigned(blk[digitNum]));
70 BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) {
73 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.";
75 // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
78 // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index',
79 // also known as an `unsigned int'. Some compilers warn without this cast.
80 len = Index(s.length());
83 Index digitNum, symbolNumInString;
84 for (digitNum = 0; digitNum < len; digitNum++) {
85 symbolNumInString = len - 1 - digitNum;
86 char theSymbol = s[symbolNumInString];
87 if (theSymbol >= '0' && theSymbol <= '9')
88 blk[digitNum] = theSymbol - '0';
89 else if (theSymbol >= 'A' && theSymbol <= 'Z')
90 blk[digitNum] = theSymbol - 'A' + 10;
91 else if (theSymbol >= 'a' && theSymbol <= 'z')
92 blk[digitNum] = theSymbol - 'a' + 10;
94 throw "BigUnsignedInABase(std::string, Base): Bad symbol in input. Only 0-9, A-Z, a-z are accepted.";
99 BigUnsignedInABase::operator std::string() const {
101 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.";
103 return std::string("0");
104 char *s = new char[len + 1];
106 Index digitNum, symbolNumInString;
107 for (symbolNumInString = 0; symbolNumInString < len; symbolNumInString++) {
108 digitNum = len - 1 - symbolNumInString;
109 Digit theDigit = blk[digitNum];
111 s[symbolNumInString] = char('0' + theDigit);
113 s[symbolNumInString] = char('A' + theDigit - 10);
116 // 2006.05.03: This needs to be [] to match the allocation