/// <summary> /// Parses the string argument as a signed integer in the radix specified by the second argument. /// </summary> /// /// <param name="S">The String containing the integer representation to be parsed</param> /// <param name="Radix">The radix to be used while parsing</param> /// /// <returns>The integer represented by the string argument in the specified radix</returns> internal static int ParseInt(String S, int Radix) { if (S == null) { throw new FormatException("null"); } if (Radix < (int)CharConsts.MIN_RADIX) { throw new FormatException("radix " + Radix + " less than Character.MIN_RADIX"); } if (Radix > (int)CharConsts.MAX_RADIX) { throw new FormatException("radix " + Radix + " greater than Character.MAX_RADIX"); } int result = 0; bool negative = false; int i = 0, len = S.Length; int limit = -(int)IntConsts.MAX_VALUE; int multmin; int digit; if (len > 0) { char firstChar = CharUtils.CharAt(S, 0); if (firstChar < '0') { // Possible leading "+" or "-" if (firstChar == '-') { negative = true; limit = (int)IntConsts.MIN_VALUE; } else if (firstChar != '+') { throw new FormatException(); } if (len == 1) { throw new FormatException("Cannot have lone + or -"); } i++; } multmin = limit / Radix; while (i < len) { // Accumulating negatively avoids surprises near MAX_VALUE digit = (int)Char.GetNumericValue(CharUtils.CharAt(S, i++)); if (digit < 0) { throw new FormatException(); } if (result < multmin) { throw new FormatException(); } result *= Radix; if (result < limit + digit) { throw new FormatException(); } result -= digit; } } else { throw new FormatException(); } return(negative ? result : -result); }
/// <summary> /// Constructs a new MathContext from a string. /// <para>The string has to specify the precision and the rounding mode to be used and has to follow the following syntax: /// "Precision=<Precision> RoundingMode=<RoundingMode>" /// This is the same form as the one returned by the ToString method.</para> /// </summary> /// /// <param name="Value">A string describing the precision and rounding mode for the new MathContext</param> /// /// <exception cref="ArgumentException">Thrown if the string is not in the correct format or if the Precision specified is < 0</exception> public MathContext(String Value) { char[] charVal = Value.ToCharArray(); int i; // Index of charVal int j; // Index of chRoundingMode int digit; // It will contain the digit parsed if ((charVal.Length < 27) || (charVal.Length > 45)) { throw new ArgumentException("Bad string format!"); } // Parsing "precision=" String for (i = 0; (i < _chPrecision.Length) && (charVal[i] == _chPrecision[i]); i++) { ; } if (i < _chPrecision.Length) { throw new ArgumentException("bad string format!"); } // Parsing the value for "precision="... digit = CharUtils.ToDigit(charVal[i], 10); if (digit == -1) { throw new ArgumentException("bad string format!"); } this._precision = this._precision * 10 + digit; i++; do { digit = CharUtils.ToDigit(charVal[i], 10); if (digit == -1) { if (charVal[i] == ' ') { // It parsed all the digits i++; break; } // It isn't a valid digit, and isn't a white space throw new ArgumentException("Bad string format!"); } // Accumulating the value parsed this._precision = this._precision * 10 + digit; if (this._precision < 0) { throw new ArgumentException("Bad string format!"); } i++; } while (true); // Parsing "roundingMode=" for (j = 0; (j < _chRoundingMode.Length) && (charVal[i] == _chRoundingMode[j]); i++, j++) { ; } if (j < _chRoundingMode.Length) { throw new ArgumentException("Bad string format!"); } // Parsing the value for "roundingMode"... this._roundingMode = (RoundingModes)Enum.Parse(typeof(RoundingModes), new string(charVal, i, charVal.Length - i), true); }
/// <summary> /// Returns a string containing a string representation of this BigInteger with base radix. /// <para>If Radix < CharHelper.MIN_RADIX} or Radix > CharHelper.MAX_RADIX then a decimal representation is returned. /// The CharHelpers of the string representation are generated with method CharHelper.forDigit.</para> /// </summary> /// /// <param name="Value">The value to convert</param> /// <param name="Radix">Base to be used for the string representation</param> /// /// <returns>Returns a string representation of this with radix 10</returns> internal static string BigInteger2String(BigInteger Value, int Radix) { int sign = Value._sign; int numberLength = Value._numberLength; int[] digits = Value._digits; if (sign == 0) { return("0"); //$NON-NLS-1$ } if (numberLength == 1) { int highDigit = digits[numberLength - 1]; long v = highDigit & 0xFFFFFFFFL; // Long.ToString has different semantic from C# for negative numbers if (sign < 0) { return("-" + Convert.ToString(v, Radix)); } return(Convert.ToString(v, Radix)); } if ((Radix == 10) || (Radix < CharUtils.MIN_RADIX) || (Radix > CharUtils.MAX_RADIX)) { return(Value.ToString()); } double bitsForRadixDigit; bitsForRadixDigit = System.Math.Log(Radix) / System.Math.Log(2); int resLengthInChars = (int)(Value.Abs().BitLength / bitsForRadixDigit + ((sign < 0) ? 1 : 0)) + 1; char[] result = new char[resLengthInChars]; int currentChar = resLengthInChars; int resDigit; if (Radix != 16) { int[] temp = new int[numberLength]; Array.Copy(digits, 0, temp, 0, numberLength); int tempLen = numberLength; int charsPerInt = DigitFitInInt[Radix]; int i; // get the maximal power of radix that fits in int int bigRadix = BigRadices[Radix - 2]; while (true) { // divide the array of digits by bigRadix and convert remainders // to CharHelpers collecting them in the char array resDigit = Division.DivideArrayByInt(temp, temp, tempLen, bigRadix); int previous = currentChar; do { result[--currentChar] = CharUtils.ForDigit(resDigit % Radix, Radix); } while (((resDigit /= Radix) != 0) && (currentChar != 0)); int delta = charsPerInt - previous + currentChar; for (i = 0; i < delta && currentChar > 0; i++) { result[--currentChar] = '0'; } for (i = tempLen - 1; (i > 0) && (temp[i] == 0); i--) { ; } tempLen = i + 1; if ((tempLen == 1) && (temp[0] == 0)) // the quotient is 0 { break; } } } else { // radix == 16 for (int i = 0; i < numberLength; i++) { for (int j = 0; (j < 8) && (currentChar > 0); j++) { resDigit = digits[i] >> (j << 2) & 0xf; result[--currentChar] = CharUtils.ForDigit(resDigit, 16); } } } while (result[currentChar] == '0') { currentChar++; } if (sign == -1) { result[--currentChar] = '-'; } return(new String(result, currentChar, resLengthInChars - currentChar)); }