BigInteger.ToString C# (CSharp) Method

ToString() public method

public ToString ( int radix ) : string
radix int
return string
        public string ToString(int radix)
        {
                if(radix < 2 || radix > 36)
                        throw (new ArgumentException("Radix must be >= 2 and <= 36"));

                string charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                string result = "";

                BigInteger a = this;

                bool negative = false;
                if((a.data[maxLength-1] & 0x80000000) != 0)
                {
                        negative = true;
                        try
                        {
                                a = -a;
                        }
                        catch(Exception) {}
                }

                BigInteger quotient = new BigInteger();
                BigInteger remainder = new BigInteger();
                BigInteger biRadix = new BigInteger(radix);

                if(a.dataLength == 1 && a.data[0] == 0)
                        result = "0";
                else
                {
                        while(a.dataLength > 1 || (a.dataLength == 1 && a.data[0] != 0))
                        {
                                singleByteDivide(a, biRadix, quotient, remainder);

                                if(remainder.data[0] < 10)
                                        result = remainder.data[0] + result;
                                else
                                        result = charSet[(int)remainder.data[0] - 10] + result;

                                a = quotient;
                        }
                        if(negative)
                                result = "-" + result;
                }

                return result;
        }

Same methods

BigInteger::ToString ( ) : string

Usage Example

Ejemplo n.º 1
0
    /*
        Write a method that adds two positive integer numbers represented as
        arrays of digits (each array element arr[i] contains a digit; the last
        digit is kept in arr[0]). Each of the numbers that will be added
        could have up to 10 000 digits.
    */
    static BigInteger AddNumbers(BigInteger num1, BigInteger num2)
    {
        if (num1.ToString().Length < num2.ToString().Length)
        {
            BigInteger tempNum = num1;

            num1 = num2;
            num2 = tempNum;
        }

        char[] longerArr = num1.ToString().ToCharArray();       // Inserting the number in char array
        Array.Reverse(longerArr);

        char[] shorterArr = num2.ToString().ToCharArray();      // Inserting the number in char array
        Array.Reverse(shorterArr);

        byte[] summedNum = new byte[longerArr.Length + 1];

        for (int i = 0; i < shorterArr.Length; i++)
        {
            byte rest = 0;
            byte digitInMind = 0;        // We can have only 0 or 1 in mind because we sum the digits from 0 to 9

            // If the sum of the current digits is bigger than 10, we will
            // have 1 in mind. In the other case we will just sum them.
            if ((Convert.ToInt32(longerArr[i]) +                // Subtract 96 because it sum the ASCII codes and we have two
                Convert.ToInt32(shorterArr[i]) - 96) > 9)       // numbers. They ASCII code of the numbers starts from 48. So 2x48
            {
                digitInMind = 1;
                rest = (byte)((Convert.ToInt32(longerArr[i]) + Convert.ToInt32(shorterArr[i]) - 96) % 10);
            }
            else
            {
                rest = (byte)(Convert.ToInt32(longerArr[i]) + Convert.ToInt32(shorterArr[i] - 96));
            }

            summedNum[i] += rest;                   // Add the result to cell in the summed array
            summedNum[i + 1] += digitInMind;        // Add the "1" in mind to next cell in the summed array
        }

        // Adding the rest digits of the longer num
        // when shorter num has no more digits.
        for (int i = shorterArr.Length; i < longerArr.Length; i++)
        {
            summedNum[i] = (byte)(longerArr[i] - 48);
        }

        Array.Reverse(summedNum);       // Reversing the number to get in the correct order

        string result = "";

        // Putting every digit in 1 string
        for (int i = 0; i < summedNum.Length; i++)
        {
            result += summedNum[i];
        }

        return BigInteger.Parse(result);       // Convert and return the string as number. Also removes the 0 in front.
    }
All Usage Examples Of BigInteger::ToString