C# - Array.copy 目标数组不够长

C# - Array.copy target array not long enough

我正在尝试将一个字节数组复制到另一个(较小的)数组中,以过滤掉电报的最后一个字节。

代码:

int length = 0;
length = streamLMS.EndRead(x); //write the length of the telegram
byte[] Recvtelegram = new byte[(length-1)];

Array.Copy(LMSRecvBuffer, 0, Recvtelegram, 0, length);
  //Where LMSRecvBuffer is the sourceArray, 0 is the sourceIndex and 
  //destinationIndex and Recvtelegram is the destinationArray.

程序在不从长度中减去 1 时工作,但我想减去 1 个字节以过滤掉最后一个字节。

调试时出错: 'System.ArgumentException' 类型的异常发生在 mscorlib.dll 中,但未在用户代码中处理。目标矩阵不够长。

有谁知道如何解决这个问题?

提前致谢

试试这个

int length = 0;
length = streamLMS.EndRead(x); //write the length of the telegram
byte[] Recvtelegram = new byte[(length-1)];

Array.Copy(LMSRecvBuffer, 0, Recvtelegram, 0, length-1);