/// <summary> /// Default constructor, intializing the Base10BigInteger with zero. /// </summary> public Base10BigInteger() { digits = new DigitContainer(); size = 1; digits[size] = 0; sign = Sign.Positive; }
/// <summary> /// Constructor creating a new Base10BigInteger as a conversion of a regular base-10 long. /// </summary> /// <param name="n">The base-10 long to be converted</param> public Base10BigInteger(long n) { digits = new DigitContainer(); sign = Sign.Positive; if (n == 0) { size = 1; digits[size] = 0; } else { if (n < 0) { n = -n; sign = Sign.Negative; } size = 0; while (n > 0) { digits[size] = n % NumberBase; n /= NumberBase; size++; } } }
/// <summary> /// Constructor creating a BigInteger instance out of a base-10 formatted string. /// </summary> /// <param name="numberString">The base-10 formatted string.</param> /// <exception cref="BigIntegerException">Invalid numeric string exception</exception> public BigInteger(string numberString) { BigInteger number = new BigInteger(); Sign numberSign = Sign.Positive; int i; for (i = 0; i < numberString.Length; i++) { if ((numberString[i] < '0') || (numberString[i] > '9')) { if ((i == 0) && (numberString[i] == '-')) { numberSign = Sign.Negative; } else { throw new BigIntegerException("Invalid numeric string.", null); } } else { number = number * Ten + long.Parse(numberString[i].ToString()); } } sign = numberSign; digits = new DigitContainer(); size = number.size; for (i = 0; i < number.size; i++) { digits[i] = number.digits[i]; } }
/// <summary> /// Constructor creating a positive BigInteger by extracting it's digits from a given byte array. /// </summary> /// <param name="byteArray">The byte array</param> /// <exception cref="BigIntegerException">The byte array's content exceeds the maximum size of a BigInteger /// exception</exception> public BigInteger(byte[] byteArray) { if (byteArray.Length / 4 > MaxSize) throw new BigIntegerException("The byte array's content exceeds the maximum size of a BigInteger.", null); digits = new DigitContainer(); sign = Sign.Positive; for (int i = 0; i < byteArray.Length; i += 2) { int currentDigit = (int)byteArray[i]; if (i + 1 < byteArray.Length) { currentDigit <<= 8; currentDigit += (int)byteArray[i + 1]; } digits[size++] = (long)currentDigit; } bool reducible = true; while ((size - 1 > 0) && (reducible == true)) { if (digits[size - 1] == 0) size--; else reducible = false; } }
/// <summary> /// Constructor creating a new BigInteger as a copy of an existing BigInteger. /// </summary> /// <param name="n">The BigInteger to be copied</param> public BigInteger(BigInteger n) { digits = new DigitContainer(); size = n.size; sign = n.sign; for (int i = 0; i < n.size; i++) digits[i] = n.digits[i]; }
/// <summary> /// Constructor deserializing a BigInteger. /// </summary> private BigInteger(SerializationInfo info, StreamingContext context) { bool signValue = (bool)info.GetValue("sign", typeof(bool)); if (signValue == true) sign = Sign.Positive; else sign = Sign.Negative; size = (int)info.GetValue("size", typeof(short)); digits = new DigitContainer(); int i; for (i = 0; i < size; i++) digits[i] = (long)(info.GetValue(string.Format("{0}{1}", "d_", i.ToString()), typeof(ushort))); }
/// <summary> /// Constructor creating a new Base10BigInteger as a copy of an existing Base10BigInteger. /// </summary> /// <param name="n">The Base10BigInteger to be copied</param> public Base10BigInteger(Base10BigInteger n) { digits = new DigitContainer(); size = n.size; sign = n.sign; for (int i = 0; i < n.size; i++) digits[i] = n.digits[i]; }