-3

Code :

public static int GetInt(byte[] bytes, int offset) => (bytes[offset] | bytes[++offset] << 8 | bytes[++offset] << 16 | bytes[++offset] << 24);

Error :

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

I searched internet but I don't have managed to solve it.

ccmorataya
  • 69
  • 2
  • 9
  • Could be [off by one](https://en.wikipedia.org/wiki/Off-by-one_error) error. Set a breakpoint and see if method is called with wrong (too big by one) `offset`. – Sinatr Nov 07 '18 at 15:32

3 Answers3

1

The byte array you are passing does not have offset+3 elements.

osoblanco
  • 468
  • 2
  • 10
1

You probably going beyond the range of the input array, as said by the exception. Check if the index is within the array bounds before acessing it, and try using fixed indexes instead of incrementing it:

public static int getByte(byte[] bytes, int index) {
    int result = 0;
    if(index < bytes.Length)
        result += bytes[index];
    else
        return result;
    if((index+1) < bytes.Length)
        result = (result << 8) + bytes[index+1];
    else
        return result;
    if((index+2) < bytes.Length)
        result = (result << 8) + bytes[index+2];
    else
        return result;
    if((index+3) < bytes.Length)
        result = (result << 8) + bytes[index+3];
    else
        return result;

    return result;
}
  • 1
    You probably don't want to return the constructed int if you couldn't actually get all four bytes. Throwing an exception in this situation is probably right since almost certainly it indicates the wrong thing has been passed into the method. ie constructing an int when you don't have all the bytes is almost certainly just going to mask actual errors. – Chris Nov 07 '18 at 15:57
  • Depends on the application and the byte array original parser, it could be truncating the value. If the byte array is surely to return 4 bytes per int then definitely discard the array, throw back an error or treat the exception. – Otavio Borges Dec 18 '18 at 17:24
-2

My best guess is this:

++offset

There are two increment styles and they are not the same.

++offset increases the value before using it.

offset++ increases teh value after using it.

I actually had a friend of mine do the same mistake, wich result in him ending up initializing the value with -1. Rather then just fixing the increment to the proper type.

You are also incrementing offset trice in taht one line. So each value will be different. Make sure that is what you really want.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • irrelevant ... [the code works for right array length](https://dotnetfiddle.net/qhhYl9) – Selvin Nov 07 '18 at 15:29
  • You think a faulty incrementing of the Indexer is **not** a mistake?`Wich part off "off by one error" did you not understand? – Christopher Nov 07 '18 at 15:33
  • 1
    why would he want `offset++` ? He uses `++offset` like `[offset+1]` `[offset+2]` `[offset+3]` – slow Nov 07 '18 at 15:44
  • 1
    The code seems to be getting four consecutive bytes in the byte array to form an int which seems perfectly reasonable so I am pretty sure he does want to be incrementing multiple times in the same row and also that he did want to increment before using or he'd be getting the first byte twice and then two more rather than four different bytes. So while your points are in general valid things to check when you have an exception of this type I don't think your comments are relevant in this specific case. – Chris Nov 07 '18 at 15:53