c# -数组.复制目标阵列不够长

本文关键字:阵列 不够 目标 复制 数组 | 更新日期: 2023-09-27 18:11:20

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

代码:

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"的异常。在mscorlib.dll中发生了ArgumentException,但没有在用户代码中处理。目标矩阵不够长。

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

Thanks in advance

c# -数组.复制目标阵列不够长

试试这个

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