I would call this function keepFirstXBytes(...). It seems to cut out the first part of a byte[] and return those bytes as an integer.
- Like if your input is:
{ 0x01, 0x02, 0x03 }, and your length is 2, then it will output only the first 2 bytes. 0x0102.
There are some caveats.
- The maximum value of
length is 4. If you take a longer value, then it will just act as if it was 4. It will just take the first 4 bytes of the array. An integer can hold only 4 bytes after all.
How I would write it:
I'm wondering if it could be easier to read, if it used an arraycopy.
byte[] destination = new byte[length];
System.arraycopy(input, 0, destination, 0, length);
And then finally convert that to an integer. Which is kind of tricky for an array of variable length. So, I would borrow the function which I found here.
public static int pareAsBigEndianByteArray(byte[] bytes) {
int factor = bytes.length - 1;
int result = 0;
for (int i = 0; i < bytes.length; i++) {
if (i == 0) {
result |= bytes[i] << (8 * factor--);
} else {
result |= bytes[i] << (8 * factor--);
}
}
return result;
}
Seems like the code of the function given in your question does those 2 things in one go, which could be more efficient, but it looks really hard to read.
EDIT:
It's easier if you just always copy it to an array of length 4.
public static int decodeInt(byte[] input, int length) {
length = java.lang.Math.min(4, length);
byte[] destination = new byte[4];
System.arraycopy(input, 0, destination, 4-length, length);
int value = java.nio.ByteBuffer.wrap(destination).getInt();
return value;
}
value += (0 & 0x000000FF) << shift;is the same asvalue += 0, thus a no-op.