Vandalious
Vandalious

Reputation: 13

Change an int type into char type in C#

I am trying to break very large numbers digit by digit and add them one by one (for a large numbers calculator).

The conditions are I should not be using any Math or BigNumbers libraries and the inputs must be taken in Strings.

The way I'm doing it is by taking the strings, putting each one into a List of (char), and converting each (char) to (int) and do the operations then change the resulting digit back to (char) and then add it to a list I named Result.

I want to know how I should change (int) to (char)? "Convert.ToChar()" does not work as it converts the number to its Unicode character. Here's the code and I marked the problems:

bool carry = false;
        int i = bigNumberLength - 1;
        List<char> result = new List<char>();
        char currentDigit;
        for (int j = smallNumberLength - 1; j >= 0; j--)
        {
            int tempDigit1 = (int)Char.GetNumericValue(bigNumber[i]);
            int tempDigit2 = (int)Char.GetNumericValue(smallNumber[j]);
            if (tempDigit1 + tempDigit2 < 10)
            {
                if (!carry)
                {
                    currentDigit = Convert.ToChar(tempDigit1 + tempDigit2);//this one
                    carry = false;
                }
                else
                {
                    if (tempDigit1 + tempDigit2 + 1 < 10)
                        currentDigit = Convert.ToChar(tempDigit1 + tempDigit2 + 1);//this one
                    else
                        currentDigit = Convert.ToChar(0); //this one
                }
                result.Add(currentDigit);
            }
            else
            {
                currentDigit = Convert.ToChar((tempDigit1 + tempDigit2) - 10); //this one
                carry = true;
                result.Add(currentDigit);
            }
            i--;
        }

Upvotes: 1

Views: 138

Answers (1)

spzvtbg
spzvtbg

Reputation: 1024

For an example I have done the addition and used just string but you could replace it with the logic for list and do the other math on the same way with some changes:

    private const char O = '0';

    public static void Main()
    {
        var num1 = "55555555555555555555555555555555555555578955555555555555";
        var num2 = "55555555555555555555555555";
        var result = string.Empty;
        var num1Index = num1.Length - 1;
        var num2Index = num2.Length - 1;
        var temp = 0;

        while (true)
        {
            if (num1Index >= 0 && num2Index >= 0)
            {
                var sum = ((temp + num1[num1Index--] - O) + (num2[num2Index--] - O));
                result = sum % 10 + result;
                temp = sum / 10;
            }
            else if (num1Index < 0 && num2Index >= 0)
            {
                result = temp + (num2[num2Index--] - O) + result;
                temp = 0;
            }
            else if (num1Index >= 0 && num2Index < 0)
            {
                result = temp + (num1[num1Index--] - O) + result;
                temp = 0;
            }
            else
            {
                break;
            }
        }

        Console.WriteLine(result);
    }

the first if statement do the actual math the rest just appends the rest of the numbers case they are with different length.

and the outpud produced by the script: enter image description here

and int to char again you can convert by adding for example

(char)([some digit hier] + '0')

Upvotes: 1

Related Questions