c# - 将 double 转换为 int 数组

标签 c# arrays

我正在尝试将 TextBox 中的每个字母与数组中的每个值进行异或。 问题是,当我将 double 转换为 int 数组时,我的 int 数组 result 仅存储一个值。

如果我运行我的代码,我会得到第一个字母异或,但如果我输入多个字母,我会收到消息:

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

我尝试自己创建一个 int 数组,例如: int[] result = new int[] {1,2,3,4,5,6,7}; 但我没有'对最多 7 个字母进行异或运算没有任何问题..

        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;  
        }

最佳答案

        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;
        }

当文本长度为2时,first_value索引可以从0..1开始。 i会从0循环到1,计算出的索引变为1 x 2 = 2,超出了索引范围。

关于c# - 将 double 转换为 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29721943/

相关文章:

mysql - SQL IN(SELECT 语句),其中匹配的 SELECT 语句是一个数组,这可能吗?

javascript - 如何使用 jquery 和 javascript 从对象数组中获取值

c# - 根据数据库查找表中的值自动创建枚举?

c# - '<name>' 中的未知列 'where clause'

Java Final 字段异端 : am I reasoning correctly?

javascript - 在 Javascript 中识别一维数组的边缘情况

PHP MySQL : Delete row if its not present in API data

c# - XAML - 获取 ListView 以使用网格单元格的全高

c# - Visual Studio C# 共享项目中的静态文件(JS、CSS 等)

javascript - 如何比较 2 个数组,从数组的匹配中返回键以重写第一个数组中的键