这是我的代码plz帮助我对此我不熟悉 [英] This is my code plz help me im new to this

查看:110
本文介绍了这是我的代码plz帮助我对此我不熟悉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)。

  for  int  i = < span class =code-digit> 0 ; i <  IsoMessageReceived.Length; i ++)
{
IsoMessageForProcessing [i + 2 ] = IsoMessageReceived [i];
}



2)。

  int 长度= Convert.ToInt32(输入[ 0 ])*  256  + Convert.ToInt32(输入[ 1 ]); 

byte [] ResultBytes = new 字节 [长度];
string ResultOutput = string .Empty;

for int i = 0 ; i < 长度; i ++)
{
ResultBytes [i] =输入[i + 2 ]; // 输入[i + 2]
}



以上2个代码抛出错误



这是错误:索引超出了数组范围



我尝试过的事情:



我正在调试这段代码,但是给我发错了

解决方案

看看你在做什么当你的循环守卫适合 ResultBytes 时,没有任何东西可以说有多大输入数组是。如果它不足以应付长度+ 2,你将在阵列结束时运行该异常。



我'#d建议您查看Input及其来源:因为它是字节,我猜您使用的是SerialPort并且尚未收到整个消息...


您是在 i 上运行,但是也使用 i + 2 ...

让我们想象一下你有5个成员,在这种情况下你将拥有这些索引...

 i  - > 0 1 2 3 4 
i + 2 - > 2 3 4 5 6



5和6是超出界限的索引!!!


对于第一个代码块, IsoMessageForProcessing 数组的大小必须至少为 IsoMessageReceived.Length + 2 。您没有显示定义/调整数组的代码。但它不够大,因为你得到一个错误。



类似于第二种情况:输入数组必须至少大小长度+ 2 。假设它是一个字节数组缓冲区,并且因为长度是从前两个字节计算的,所以它的大小应该是0x10002,以避免任何超出范围的错误。


1).

for (int i = 0; i < IsoMessageReceived.Length; i++)
{
  IsoMessageForProcessing[i + 2] = IsoMessageReceived[i];
}


2).

int Length = Convert.ToInt32(Input[0]) * 256 + Convert.ToInt32(Input[1]);

byte[] ResultBytes = new byte[Length];
string ResultOutput = string.Empty;

for (int i = 0; i < Length; i++)
{
  ResultBytes[i] = Input[i + 2];        //Input[i + 2]
}


The above 2 codes are throwing the error

This is the error : Index was outside the bounds of the array

What I have tried:

i'm debugging this code but throwing me error

解决方案

Look at what you are doing While your loop guard is right for ResultBytes, there is nothing there that says how big the Input array is. If it isn't long enough to cope with the Length + 2, you will get that exception as you run off the end of the array.

I'#d suggest that you look at Input and where it comes from: since it's bytes, I'd guess you are using a SerialPort and haven't received the entire message yet...


You are running on i, but using i + 2 too...
Let imagine that you have 5 members in this case you will have these indexes...

i     -> 0  1  2  3  4
i + 2 -> 2  3  4  5  6


5 and 6 are indexes that out of the bound!!!


For the first code block , the size of the IsoMessageForProcessing array must be at least IsoMessageReceived.Length + 2. You did not show the code where the array is defined / sized. But it is not large enough because you get an error.

Similar for the second case: The Input array must be at least of size Length + 2. Assuming it is a byte array buffer and because the length is calculated from the first two bytes, it should be of size 0x10002 to avoid any out of bound errors.


这篇关于这是我的代码plz帮助我对此我不熟悉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆