2

I'm trying to XOR each letter from my TextBox with each value from my array. The problem is, that when I convert double to int array, my int array result stores only one value.

If I run my code I get first letter XORed, but if I input more then one, I get message :

System.IndexOutOfRangeException: Index was outside the bounds of the array.

I have tried myself to create an int array like: int[] result = new int[] {1,2,3,4,5,6,7}; and I didn't have any problems with XORing up to 7 letters..

        private void iTalk_Button_12_Click(object sender, EventArgs e)
        {

            ambiance_RichTextBox1.Text = XorText(ambiance_RichTextBox1.Text);
        }
        private string XorText(string text)
        {

            string newText = "";
            double r = 3.9;
            double[] first_value = new double[text.Length];
            double[] to_int_array = new double[text.Length];
            for (int i = 0; i < text.Length; i++)
            {
                double get_first = r * i * (1 - i);
                int index = (int)(i * text.Length);
                first_value[index] = get_first;
            }

            for (int i = 0; i < text.Length; i++)
            {

                int xnbb = 0;
                if (first_value[i] > Math.Exp(Math.Log(2) * (-i)))
                {
                    double get_first = first_value[i] - Math.Exp(Math.Log(2) * (-i));
                    xnbb = 1;
                }

                double array_of_values = xnbb + 1 * Math.Round(Math.Exp(Math.Log(2) * (24 - i)));

                int index = (int)(i * text.Length);
                to_int_array[index] = array_of_values;
                int[] result = new int[] { Convert.ToInt32(to_int_array[i]) };


                int charValue = Convert.ToInt32(text[i]);
                charValue ^= result[i]%320;
                newText += char.ConvertFromUtf32(charValue);
            }
            return newText;  
        }
1
  • Throw away this code and start over with a flowchart. I don't believe there's a single line of code here that makes sense. Oh alright, maybe return newText; can stay. Commented Apr 18, 2015 at 20:06

2 Answers 2

1
        double[] first_value = new double[text.Length];
        ... 
        for (int i = 0; i < text.Length; i++)
        {
            double get_first = r * i * (1 - i);
            int index = (int)(i * text.Length);
            first_value[index] = get_first;
        }

When the text length is 2, first_value index may run from 0..1. i will loop from 0 to 1. the calculated index becomes 1 x 2 = 2, and that is beyond the index range.

Sign up to request clarification or add additional context in comments.

Comments

1

When passing a string with 2 characters to XorText, the System.IndexOutOfRangeException is thrown in this Line:

 first_value[index] = get_first;

because the index is 2 when the loop body is executed the second time:

 int index = (int)(i * text.Length);

You really should consider learning how to use a debugger. It will make programming easier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.