C# 在C中将int类型更改为char类型#

C# 在C中将int类型更改为char类型#,c#,char,integer,calculator,data-conversion,C#,Char,Integer,Calculator,Data Conversion,我正在尝试将非常大的数字逐位分解,然后逐个相加(对于大数字计算器) 条件是我不应该使用任何数学或BigNumbers库,输入必须是字符串 我这样做的方式是,将字符串放入(char)列表中,将每个(char)转换为(int),然后执行操作,将结果数字更改回(char),然后将其添加到我命名的结果列表中 我想知道如何将(int)更改为(char)?“Convert.ToChar()”无法工作,因为它将数字转换为Unicode字符。 这是代码,我标记了问题: bool carry = false;

我正在尝试将非常大的数字逐位分解,然后逐个相加(对于大数字计算器)

条件是我不应该使用任何数学或BigNumbers库,输入必须是字符串

我这样做的方式是,将字符串放入
(char)
列表中,将每个
(char)转换为(int)
,然后执行操作,将结果数字更改回
(char)
,然后将其添加到我命名的结果列表中

我想知道如何将(int)更改为(char)?“Convert.ToChar()”无法工作,因为它将数字转换为Unicode字符。 这是代码,我标记了问题:

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--;
        }
bool进位=false;
int i=bigNumberLength-1;
列表结果=新列表();
字符位;
对于(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)
{
如果(!进位)
{
currentDigit=Convert.ToChar(tempDigit1+tempDigit2);//这个
进位=假;
}
其他的
{
if(tempDigit1+tempDigit2+1<10)
currentDigit=Convert.ToChar(tempDigit1+tempDigit2+1);//这个
其他的
currentDigit=Convert.ToChar(0);//这个
}
结果。添加(当前数字);
}
其他的
{
currentDigit=Convert.ToChar((tempDigit1+tempDigit2)-10);//这个
进位=真;
结果。添加(当前数字);
}
我--;
}

例如,我已经做了加法,只使用了字符串,但您可以将其替换为列表的逻辑,并以相同的方式进行其他数学运算,但需要做一些更改:

    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);
    }

请提供您当前的代码以及您在哪里遇到问题。我假设您正在寻找使用
int.Parse
?@DavidG的库。我倾向于理解,这应该在没有
int.Parse
或类似内容的情况下解决。当您开始谈论转换回char时,我迷路了。您试图解决的问题是,给定一个数字的字符串表示形式,将所有数字相加。为什么要将任何内容转换回
char
,您必须提供的答案是一个和。@在库之间,我假设它们不允许使用
biginger
之类的内容,而不是BCLOW中的内容!这是非常好的工作,它是更好的方式和清洁比我的想法。我需要多读一点你的算法来理解你到底做了什么。非常感谢你!我不能将它标记为问题的答案,因为它可能会让将来想知道如何直接(int)到(char)的其他人感到困惑,但它确实是完成手头任务的更好方法。哦,我刚刚注意到你的编辑,你也回答了问题本身。现在标记它!
(char)([some digit hier] + '0')