Exemplo n.º 1
0
        public override void ToBigInt(string Str)
        {
            BigInt ResultBigInt = new BigInt(this.Size, 0);
            string HString      = "0123456789ABCDEF";
            string StrUpper     = Str.ToUpper();

            if (IsNumeric(StrUpper))
            {
                if (StrUpper.StartsWith("+") || StrUpper.StartsWith("-"))
                {
                    StrUpper = StrUpper.Substring(1, StrUpper.Length - 1);
                }

                for (int i = 0; i < StrUpper.Length; i++)
                {
                    char cellVal = StrUpper[i];
                    int  n       = HString.IndexOf(cellVal);
                    if (n == -1)
                    {
                        throw (new ArgumentException("Invalid string in constructor."));
                    }
                    else
                    {
                        ResultBigInt = ResultBigInt.Mul10() + (uint)n;
                    }
                }
                ResultBigInt.Reduce();
            }
            else if (StrUpper.Contains('O'))
            {
                int xPos = StrUpper.IndexOf("O");
                if (xPos != -1)
                {
                    StrUpper = StrUpper.Substring(xPos + 1, (StrUpper.Length - xPos - 1));
                }
                for (int i = 0; i < StrUpper.Length; i++)
                {
                    char cellVal = StrUpper[i];
                    int  n       = HString.IndexOf(cellVal);
                    if (n == -1)
                    {
                        throw (new ArgumentException("Invalid string in constructor."));
                    }
                    else
                    {
                        ResultBigInt = ResultBigInt.Mul8() + (uint)n;
                    }
                }
                ResultBigInt.Reduce();
            }
            else
            {
                int xPos = StrUpper.IndexOf("X");
                if (xPos != -1)
                {
                    StrUpper = StrUpper.Substring(xPos + 1, (StrUpper.Length - xPos - 1));
                }
                for (int i = 0; i < StrUpper.Length; i++)
                {
                    char cellVal = StrUpper[i];
                    int  n       = HString.IndexOf(cellVal);
                    if (n == -1)
                    {
                        throw (new ArgumentException("Invalid string in constructor."));
                    }
                    else
                    {
                        ResultBigInt = ResultBigInt.Mul16() + (uint)n;
                    }
                }
                ResultBigInt.Reduce();
            }



            /*if (Sign == signt.negative) // overflow check for overflow on negative value
             * {
             *  if ((ResultBigInt.Value[Size - 1] & HIGH_BIT_MASK) == 0)
             *      throw (new OverflowException("Size overflow, while initialising the value array."));
             * }
             * else // overflow check for overflow on positive value
             * {
             *  if ((ResultBigInt.Value[Size - 1] & HIGH_BIT_MASK) != 0)
             *      throw (new OverflowException("Size overflow, while initialising the value array."));
             * }*/

            for (int i = 0; i < ResultBigInt.Spart; i++)
            {
                this.Value[i] = ResultBigInt.Value[i];
            }

            this.Spart = ResultBigInt.Spart;


            if (this.Spart == 0)
            {
                this.Spart = 1;
            }
        }