0

The goal of this code is to take in two text box fields and use the key to encrypt the original text box fields however I keep getting problems on char.GetNumericValue(orignalText[i]); it is saying can not convert type double to int are you missing a cast? I am not sure what this means, if anyone can provide insight and also if you see anything in my code that can be improved please let me know I love to learn more and more.

{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //We want to divide the plain texty into blocks of the key size.

    private void button1_Click(object sender, EventArgs e)
    {
        //Each char should be replaced by a number.


        int keySize = textBox2.Text.Length;
        int orginalSize = textBox1.Text.Length;


        char[] orignalText = textBox1.Text.ToCharArray();
        char[] keyText = textBox2.Text.ToCharArray();

        int[] encryptedText = new int[orginalSize];
        int[] encryptedKey = new int[keySize];
        int counter = 0;
        int[] combinedText = new int[orginalSize];
        for (int i = 0; i < keySize; i++)
        {
           
            encryptedKey[i] = char.GetNumericValue(keyText[i]);

        }


        while (orginalSize != counter)
        {

            int changeVal = 0;
            for (int i = counter; i < keySize; i++)
            {
                encryptedText[i] = char.GetNumericValue(orignalText[i]);



                combinedText[i] = encryptedText[i] + encryptedKey[changeVal];

                if (encryptedText[i] +encryptedKey[changeVal] > 27)
                {
                    combinedText[i] = (int)(encryptedText[i] + encryptedKey[changeVal]) - 27;
                }

                changeVal++;
                counter += 1;   
            }

            keySize += keySize;
            
        }

        string [] encryptedTextFinal = new string[textBox1.Text.Length];

        for (int j = 0; j < textBox1.Text.Length; j++)
        {
            encryptedTextFinal[j] = Convert.ToChar(combinedText[j]);

            MessageBox.Show(encryptedTextFinal[j].ToString());
        }
    }

}

}

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Thomas
  • 65
  • 1
  • 10
  • Also for some reason, I am getting -2 in my MessageBox.Show does anyone know why this might be? I been debugging for a min and couldn't figure it out? – Thomas Dec 14 '20 at 23:23
  • Linked duplicate should explain all behavior you see, for actual code you probably want https://stackoverflow.com/questions/239103/convert-char-to-int-in-c-sharp – Alexei Levenkov Dec 14 '20 at 23:30

0 Answers0