我正在尝试使用tcp it服务,需要我以字符串形式发送一个ID。我从示例代码中获得了以下方法。 我的问题是,当我输入4个字符的数字,如“4000”,“2000”,“3000”字符串的方法工作,但当我输入字符串少于4个字符“1”,“20”或“300” 它返回System.ArgumentException:目标数组不够长
System.ArgumentException:目标数组不够长。检查 destIndex和长度以及数组的下限。
public byte[] prepNetworkStreamBuffer(string reqiiredID) {
byte[] id = UTF8Encoding.UTF8.GetBytes(reqiiredID);
int l = id.Length;
byte[] idb = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder(l));
byte[] buff = new byte[1 + 1 + id.Length + l];
buff[0] = 0;
buff[1] = (byte)VerificationServiceCommands.addIDtoAFIS;
idb.CopyTo(buff, 1 + 1);
id.CopyTo(buff, 1 + 1 + idb.Length);
return buff;
}
在异常occors: id.CopyTo(buff,1 + 1 + idb.Length); – user1576148 2012-08-04 14:15:07