- public BigInteger(String val, int radix) {
-
- int cursor = 0, numDigits;
- int len = val.length();
-
-
- if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
- throw new NumberFormatException("Radix out of range");
- if (val.length() == 0)
- throw new NumberFormatException("Zero length BigInteger");
-
- signum = 1;
- int index = val.lastIndexOf("-");
- if (index != -1) {
- if (index == 0) {
- if (val.length() == 1)
- throw new NumberFormatException("Zero length BigInteger");
- signum = -1;
- cursor = 1;
- } else {
- throw new NumberFormatException("Illegal embedded minus sign");
- }
- }
-
- while (cursor < len &&
- Character.digit(val.charAt(cursor),radix) == 0)
- cursor++;
- if (cursor == len) {
- signum = 0;
- mag = ZERO.mag;
- return;
- } else {
- numDigits = len - cursor;
- }
-
-
-
-
- int numBits = (int)(((numDigits * bitsPerDigit[radix]) >>> 10) + 1);
- int numWords = (numBits + 31) /32;
- mag = new int[numWords];
-
-
- int firstGroupLen = numDigits % digitsPerInt[radix];
- if (firstGroupLen == 0)
- firstGroupLen = digitsPerInt[radix];
-
- String group = val.substring(cursor, cursor += firstGroupLen);
- mag[mag.length - 1] = Integer.parseInt(group, radix);
- if (mag[mag.length - 1] < 0)
- throw new NumberFormatException("Illegal digit");
-
- int superRadix = intRadix[radix];
- int groupVal = 0;
- while (cursor < val.length()) {
- group = val.substring(cursor, cursor += digitsPerInt[radix]);
- groupVal = Integer.parseInt(group, radix);
- if (groupVal < 0)
- throw new NumberFormatException("Illegal digit");
- destructiveMulAdd(mag, superRadix, groupVal);
- }
- mag = trustedStripLeadingZeroInts(mag);
-
- }
BigInteger大数家法源代码及分析
原文:http://www.cnblogs.com/aishangtaxuefeihong/p/4927865.html