值超出数组范围

时间:2019-01-07 14:38:32

标签: c# arrays

我正在使用数组来存储字符串中的字符及其对应的ASCII值。我想将字符串转换为字符。使用字符来找到asciivalue。使用这些值创建一个新字母,然后使用新字母对消息进行编码。但是我一直在获取错误变量在数组的边界之外。任何帮助都将是巨大的,我将为此而努力,并于今天到期,我无法解决。任何帮助深表感谢!

 int CountKeywordLength = txtKeyword.Text.Length, i = 1, j = 1;

        string KeywordStorage = txtKeyword.Text;


        int[,] CharacterAsciiArray = new int[122, CountKeywordLength+1];


        foreach (char ch in KeywordStorage)
        {
            int CharacterLetterAscii = 0, CharacterLetterAsciiPlusOne = 0 ;

            string CharacterLetter = ch.ToString();


            if (CharacterLetter.ToLower() == "a")
            {
               CharacterLetterAscii = 97;
            }
            else if (CharacterLetter.ToLower() == "b")
            {
                CharacterLetterAscii = 98;
            }
            else if (CharacterLetter.ToLower() == "c")
            {
                CharacterLetterAscii = 99;
            }
            else if (CharacterLetter.ToLower() == "d")
            {
                CharacterLetterAscii = 100;
            }
            else if (CharacterLetter.ToLower() == "e")
            {
                CharacterLetterAscii = 101;
            }
            else if (CharacterLetter.ToLower() == "f")
            {
                CharacterLetterAscii = 102;
            }
            else if (CharacterLetter.ToLower() == "g")
            {
                CharacterLetterAscii = 103;
            }
            else if (CharacterLetter.ToLower() == "h")
            {
                CharacterLetterAscii = 104;
            }
            else if (CharacterLetter.ToLower() == "i")
            {
                CharacterLetterAscii = 105;
            }
            else if (CharacterLetter.ToLower() == "j")
            {
                CharacterLetterAscii = 106;
            }
            else if (CharacterLetter.ToLower() == "k")
            {
                CharacterLetterAscii = 107;
            }
            else if (CharacterLetter.ToLower() == "l")
            {
                CharacterLetterAscii = 108;
            }
            else if (CharacterLetter.ToLower() == "m")
            {
                CharacterLetterAscii = 109;
            }
            else if (CharacterLetter.ToLower() == "n")
            {
                CharacterLetterAscii = 110;
            }
            else if (CharacterLetter.ToLower() == "o")
            {
                CharacterLetterAscii = 111;
            }
            else if (CharacterLetter.ToLower() == "p")
            {
                CharacterLetterAscii = 112;
            }
            else if (CharacterLetter.ToLower() == "q")
            {
                CharacterLetterAscii = 113;
            }
            else if (CharacterLetter.ToLower() == "r")
            {
                CharacterLetterAscii = 114;
            }
            else if (CharacterLetter.ToLower() == "s")
            {
                CharacterLetterAscii = 115;
            }
            else if (CharacterLetter.ToLower() == "t")
            {
                CharacterLetterAscii = 116;
            }
            else if (CharacterLetter.ToLower() == "u")
            {
                CharacterLetterAscii = 117;
            }
            else if (CharacterLetter.ToLower() == "v")
            {
                CharacterLetterAscii = 118;
            }
            else if (CharacterLetter.ToLower() == "w")
            {
                CharacterLetterAscii = 119;
            }
            else if (CharacterLetter.ToLower() == "x")
            {
                CharacterLetterAscii = 120;
            }
            else if (CharacterLetter.ToLower() == "y")
            {
                CharacterLetterAscii = 121;
            }
            else if (CharacterLetter.ToLower() == "z")
            {
                CharacterLetterAscii = 122;
            }

            CharacterAsciiArray[i,j] = CharacterLetterAscii;

           CharacterLetterAsciiPlusOne = CharacterLetterAscii + 1;

                i++;
                for (i = 2; i < 24; i++)
                {
                    CharacterAsciiArray[i, j] = CharacterLetterAsciiPlusOne;
                    CharacterLetterAsciiPlusOne++;

                    if (CharacterLetterAsciiPlusOne > 122)
                    {
                        int DifferenceCharacterAscii = CharacterLetterAsciiPlusOne - 122;
                        CharacterLetterAsciiPlusOne = 65 + DifferenceCharacterAscii;
                        i++;
                    }
                }
            j++;
        }

        string MessageCharacters = txtEncryption.Text;
        int MessageLetterAscii = 0;

        int CountMessageLength = txtEncryption.Text.Length;
        int[] MessageAsciiArray = new int[CountMessageLength+1];

        foreach (char c in MessageCharacters)
        {
            string MessageCharactersStore = c.ToString(); 

            if (MessageCharactersStore.ToLower() == "a")
            {
                MessageLetterAscii = 97;
            }
            else if (MessageCharactersStore.ToLower() == "b")
            {
                MessageLetterAscii = 98;
            }
            else if (MessageCharactersStore.ToLower() == "c")
            {
                MessageLetterAscii = 99;
            }
            else if (MessageCharactersStore.ToLower() == "d")
            {
                MessageLetterAscii = 100;
            }
            else if (MessageCharactersStore.ToLower() == "e")
            {
                MessageLetterAscii = 101;
            }
            else if (MessageCharactersStore.ToLower() == "f")
            {
                MessageLetterAscii = 102;
            }
            else if (MessageCharactersStore.ToLower() == "g")
            {
                MessageLetterAscii = 103;
            }
            else if (MessageCharactersStore.ToLower() == "h")
            {
                MessageLetterAscii = 104;
            }
            else if (MessageCharactersStore.ToLower() == "i")
            {
                MessageLetterAscii = 105;
            }
            else if (MessageCharactersStore.ToLower() == "j")
            {
                MessageLetterAscii = 106;
            }
            else if (MessageCharactersStore.ToLower() == "k")
            {
                MessageLetterAscii = 107;
            }
            else if (MessageCharactersStore.ToLower() == "l")
            {
                MessageLetterAscii = 108;
            }
            else if (MessageCharactersStore.ToLower() == "m")
            {
                MessageLetterAscii = 109;
            }
            else if (MessageCharactersStore.ToLower() == "n")
            {
                MessageLetterAscii = 110;
            }
            else if (MessageCharactersStore.ToLower() == "o")
            {
                MessageLetterAscii = 111;
            }
            else if (MessageCharactersStore.ToLower() == "p")
            {
                MessageLetterAscii = 112;
            }
            else if (MessageCharactersStore.ToLower() == "q")
            {
                MessageLetterAscii = 113;
            }
            else if (MessageCharactersStore.ToLower() == "r")
            {
                MessageLetterAscii = 114;
            }
            else if (MessageCharactersStore.ToLower() == "s")
            {
                MessageLetterAscii = 115;
            }
            else if (MessageCharactersStore.ToLower() == "t")
            {
                MessageLetterAscii = 116;
            }
            else if (MessageCharactersStore.ToLower() == "u")
            {
                MessageLetterAscii = 117;
            }
            else if (MessageCharactersStore.ToLower() == "v")
            {
                MessageLetterAscii = 118;
            }
            else if (MessageCharactersStore.ToLower() == "w")
            {
                MessageLetterAscii = 119;
            }
            else if (MessageCharactersStore.ToLower() == "x")
            {
                MessageLetterAscii = 120;
            }
            else if (MessageCharactersStore.ToLower() == "y")
            {
                MessageLetterAscii = 121;
            }
            else if (MessageCharactersStore.ToLower() == "z")
            {
                MessageLetterAscii = 122;
            }

            int DifferenceCalculation = MessageLetterAscii - 97;
            int MessageAsciiValue = CharacterAsciiArray[1,j];
            if (DifferenceCalculation > 0)
            {
                 MessageAsciiValue = CharacterAsciiArray[1, j] + DifferenceCalculation;
            }
            MessageAsciiArray[i] = MessageAsciiValue;
            i++;
            j++;
            if (j > 3)
            {
                j = 1;
            }
        }
        string[] MessageArray = new string[122];
        for ( i = 1; i < CountMessageLength; i++)
        {
            if (MessageAsciiArray[i] == 97)
            {
                MessageArray[i] = "a";
            }
            else if (MessageAsciiArray[i] == 98)
            {
                MessageArray[i] = "b";
            }
            else if (MessageAsciiArray[i] == 99)
            {
                MessageArray[i] = "c";
            }
            else if (MessageAsciiArray[i] == 100)
            {
                MessageArray[i] = "d";
            }
            else if (MessageAsciiArray[i] == 101)
            {
                MessageArray[i] = "e";
            }
            else if (MessageAsciiArray[i] == 102)
            {
                MessageArray[i] = "f";
            }
            else if (MessageAsciiArray[i] == 103)
            {
                MessageArray[i] = "g";
            }
            else if (MessageAsciiArray[i] == 104)
            {
                MessageArray[i] = "h";
            }
            else if (MessageAsciiArray[i] == 105)
            {
                MessageArray[i] = "i";
            }
            else if (MessageAsciiArray[i] == 106)
            {
                MessageArray[i] = "j";
            }
            else if (MessageAsciiArray[i] == 107)
            {
                MessageArray[i] = "k";
            }
            else if (MessageAsciiArray[i] == 108)
            {
                MessageArray[i] = "l";
            }
            else if (MessageAsciiArray[i] == 109)
            {
                MessageArray[i] = "m";
            }
            else if (MessageAsciiArray[i] == 110)
            {
                MessageArray[i] = "n";
            }
            else if (MessageAsciiArray[i] == 111)
            {
                MessageArray[i] = "o";
            }
            else if (MessageAsciiArray[i] == 112)
            {
                MessageArray[i] = "p";
            }
            else if (MessageAsciiArray[i] == 113)
            {
                MessageArray[i] = "q";
            }
            else if (MessageAsciiArray[i] == 114)
            {
                MessageArray[i] = "r";
            }
            else if (MessageAsciiArray[i] == 115)
            {
                MessageArray[i] = "s";
            }
            else if (MessageAsciiArray[i] == 116)
            {
                MessageArray[i]= "t";
            }
            else if (MessageAsciiArray[i] == 117)
            {
                MessageArray[i] = "u";
            }
            else if (MessageAsciiArray[i] == 118)
            {
                MessageArray[i] = "v";
            }
            else if (MessageAsciiArray[i] == 119)
            {
                MessageArray[i] = "w";
            }
            else if (MessageAsciiArray[i] == 120)
            {
                MessageArray[i] = "x";
            }
            else if (MessageAsciiArray[i] == 121)
            {
                MessageArray[i] = "y";
            }
            else if (MessageAsciiArray[i] == 122)
            {
                MessageArray[i] = "z";
            }

            i++;

0 个答案:

没有答案