在记录 x 以在字符数组中查找后,如何阻止我的计数器从 0 变为 48?

How can I stop my counter from going to 48 from 0 after it records x for look up in a character array?

下面我包含了一段使用 polybius 方块的解密方法。我想获取前两个用户输入并将它们转换为坐标以查找正方形中的哪个字母与输入数字匹配。我的代码适用于 x 值,然后将 y 分配为 48。我需要帮助找到 x 和 y 之间的增量而不将 y 更改为 48。

public string DecryptMessage(string userInput)
    {
        string outputMessage = string.Empty;

        char[,] alphaValues = new char[5, 5] {
            {'A', 'B', 'C', 'D', 'E'},
            {'F', 'G', 'H', 'I', 'K'},
            {'L', 'M', 'N', 'O', 'P'},
            {'Q', 'R', 'S', 'T', 'U'},
            {'V', 'W', 'X', 'Y', 'Z'},
        };

        char[] userInputArray = userInput.ToCharArray();

            for (int i = 0; i < userInputArray.Length; i++)
            {
                int x = Convert.ToInt32(userInputArray[i]); i++;
            //after this point i somehow changes from 0 to 48 with the incriment
            //if i get this fixed the decrypt will work, still trying to figure this out

                int y = Convert.ToInt32(userInputArray[i]);


                char letterToReturn = alphaValues[x, y];


                string outputLetter = letterToReturn.ToString();
                outputMessage += outputLetter;

            }
        return outputMessage;
    } 

问题是您要将数字的字符表示形式转换为整数,这将是表示该字符的数值,而不是您要查找的数字。

试试这个:

...
int x = userInputArray[i++] - '0';
int y = userInputArray[i] - '0';
char letterToReturn = alphaValues[x, y];
...

由于“0”到“9”用 48 到 57 表示,从字符中减去“0”有效地将数字字符转换为实际数字。

'0' 是 48,'1' 是 49,依此类推一直到 '9' 是 57,所以 '1' - '0' 是 49 - 48 或者只是 1. '9' - '0' 是 57 - 48 或只是 9。

当然这个小技巧取决于所使用的字符集,但它适用于 ASCII 和 unicode。