目標陣列不夠長。檢查 destIndex 和長度,以及陣列的下限 <- 這是什么錯誤???
我該如何解決?添加 Array.Copy 后發生此錯誤
我不知道....
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");
}
uj5u.com熱心網友回復:
回答具體問題,復制方法的定義:
public static void Copy (
Array sourceArray, int sourceIndex,
Array destinationArray, int destinationIndex,
int length);
在你的情況下
Array.Copy(LRC, 0, hexData, hexData.Length 1, 1);
因此,您試圖將位元組復制到陣列hexData到hexData.Length 1基本上超出其邊界的索引。
我不知道您要實作什么,但是您應該確保復制期間的目標索引小于或等于hexData.Length - 1(還要考慮長度)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/389752.html
上一篇:資料到達控制器但不查看
