Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Destination array was not long enough. Check destIndex and length, and the array's lower bounds.?

Destination array was not long enough. Check destIndex and length, and the array’s lower bounds <- what is this error???

how can i fix it?
This error occur after i add Array.Copy

i have no idea….

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

static byte[] sendData = new byte[5];
  static int sendCount = 0;
    
    try
                        {
    
                            Console.Write("->");
                            string text = Console.ReadLine();
                            foreach(string s in text.Split(' '))
                            {
                                if(null != s && "" != s)
                                {
                                    sendData[sendCount++] = Convert.ToByte(s, 16);
                                }
                            }
                            byte[] LRC = new byte[1];
                            LRC = BitConverter.GetBytes((Int16)sendData[2] ^ sendData[3] ^ sendData[4]);
    
                            byte[] hexData = new byte[sendData.Length + LRC.Length];
                            Array.Copy(sendData, 0, hexData, 0, 5);//<-error occur this point
                            Array.Copy(LRC, 0, hexData, hexData.Length + 1, 1);//<-or this point
    
                            port.Write(hexData, 0, hexData.Length);
    
                            Array.Clear(sendData, 0, sendData.Length);
                            Array.Clear(LRC, 0, LRC.Length);
                            Array.Clear(hexData, 0, hexData.Length);
                            sendCount = 0;
                        }
                        catch(Exception e)
                        {
                            Console.WriteLine(e.Message);
                            Console.WriteLine("Check Data");
                        }

>Solution :

Answering on concrete question, the definition of the copy method:

public static void Copy (
    Array sourceArray, int sourceIndex,
    Array destinationArray, int destinationIndex,
    int length);

In your case

Array.Copy(LRC, 0, hexData, hexData.Length + 1, 1);

So, you are trying to copy bytes to array hexData to the index hexData.Length + 1 that is basically out of its boundaries.

I have no idea what you are trying to implement, but you should make sure that destination index during the copy is less or equal than hexData.Length - 1 (also taking into account the length).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading