我正在尝试将 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/