Destination array was not long enough. Check destIndex and length, and the array's lower bounds <- what is this error???
how can i fix it? This error occur after i add Array.Copy
i have no idea....
static byte[] sendData = new byte[5];
static int sendCount = 0;
try
{
Console.Write("->");
string text = Console.ReadLine();
foreach(string s in text.Split(' '))
{
if(null != s && "" != s)
{
sendData[sendCount ] = Convert.ToByte(s, 16);
}
}
byte[] LRC = new byte[1];
LRC = BitConverter.GetBytes((Int16)sendData[2] ^ sendData[3] ^ sendData[4]);
byte[] hexData = new byte[sendData.Length LRC.Length];
Array.Copy(sendData, 0, hexData, 0, 5);//<-error occur this point
Array.Copy(LRC, 0, hexData, hexData.Length 1, 1);//<-or this point
port.Write(hexData, 0, hexData.Length);
Array.Clear(sendData, 0, sendData.Length);
Array.Clear(LRC, 0, LRC.Length);
Array.Clear(hexData, 0, hexData.Length);
sendCount = 0;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Check Data");
}
CodePudding user response:
Answering on concrete question, the definition of the copy method:
public static void Copy (
Array sourceArray, int sourceIndex,
Array destinationArray, int destinationIndex,
int length);
In your case
Array.Copy(LRC, 0, hexData, hexData.Length 1, 1);
So, you are trying to copy bytes to array hexData
to the index hexData.Length 1
that is basically out of its boundaries.
I have no idea what you are trying to implement, but you should make sure that destination index during the copy is less or equal than hexData.Length - 1
(also taking into account the length).