简体   繁体   English

将C#中的一个int型改成char型

[英]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.条件是我不应该使用任何 Math 或 BigNumbers 库,并且输入必须采用字符串形式。

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.我这样做的方法是获取字符串,将每个字符串放入(char)的列表中,然后将每个(char) to (int)并执行操作,然后将结果数字更改回(char)然后将它添加到我命名为 Result 的列表中。

I want to know how I should change (int) to (char)?我想知道我应该如何将 (int) 更改为 (char)? "Convert.ToChar()" does not work as it converts the number to its Unicode character . “Convert.ToChar()”不起作用,因为它将数字转换为其 Unicode 字符 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--;
        }

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.第一个 if 语句进行实际数学运算 rest 只是附加数字的 rest,它们具有不同的长度。

and the outpud produced by the script:以及脚本产生的输出: 在此处输入图像描述

and int to char again you can convert by adding for example并再次将 int 转换为 char 您可以通过添加例如进行转换

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM