我尝试制作一个将十六进制值从串行转换为十进制值的接收器软件。这是我的代码:十六进制到十进制转换错误:索引超出范围。必须是非负数且小于集合的大小。参数名称:startIndex
// Obtain the number of bytes waiting in the port's buffer
int bytes = comport.BytesToRead;
// Create a byte array buffer to hold the incoming data
byte[] buffer = new byte[bytes];
// Read the data from the port and store it in our buffer
comport.Read(buffer, 0, bytes);
string hexValues = ByteArrayToHexString(buffer);
string[] hexValuesSplit = hexValues.Split(' ');
foreach (String hex in hexValuesSplit)
{
// Convert the number expressed in base-16 to an integer.
int value = Convert.ToInt32(hex, 16);
Log(LogMsgType.Incoming, value+" ppm \n");
}
但是,当我尝试发送从串行它总是说,“索引超出范围的数据必须为非负数且小于集合的大小参数名:的startIndex ”。那么我该怎么做?
哪条线会抛出错误? –
我怀疑它在'ByteArrayToHexString'中,你没有提供:( –