- Public BigInteger (String val, int radix) {
- int cursor = 0, numdigits;
- int len = Val.length (); //Gets the length of the string
- //Non-qualifying conditions
- 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");
- //positive or negative, handle the "-" inside the string
- 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");
- }
- }
- //Skip the previous 0
- While (cursor < len &&
- Character.digit (Val.charat (cursor), radix) = = 0)
- cursor++;
- if (cursor = = len) {//If the string is all 0, it is stored as Zero.mag
- Signum = 0;
- Mag = Zero.mag;
- return;
- } Else {//numdigits is actually a valid number
- Numdigits = Len-cursor;
- }
- how many bits will be required to convert the radix number of//numdigits bits into 2 binary
- The elements in the//bitsperdigit array are multiplied by 1024 and need to be shifted right 10 bits (equivalent to dividing by 1024), when doing division there will be
- //decimal loss, so add 1 to ensure that the number of digits must be sufficient
- //An int has 32bit, so dividing by 32 is the size of the mag array we started estimating
- int numbits = (int) (((Numdigits * Bitsperdigit[radix]) >>> ) + 1);
- int numwords = (numbits + + )/32;
- Mag = new int[numwords];
- //start to intercept the number in the string according to Digitsperint
- //Will not be enough Digitsperint[radix] first take out the conversion
- int firstgrouplen = numdigits% digitsperint[radix];
- if (Firstgrouplen = = 0)
- Firstgrouplen = Digitsperint[radix];
- //Put the number of the first paragraph in the last digit of the mag array
- 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");
- //The remaining segment conversion
- 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);
- }
The source code and analysis of BigInteger large number home law