Here you can find the source of toInteger(byte[] bytes, int index)
Parameter | Description |
---|---|
bytes | a parameter |
index | a parameter |
public static int toInteger(byte[] bytes, int index)
//package com.java2s; //License from project: Apache License public class Main { /** // w ww . jav a 2 s . com * Convert the necessary number of bytes starting at the given index * to an int. * * @param bytes * @param index * @return */ public static int toInteger(byte[] bytes, int index) { int val = 0; int typeBytes = Integer.SIZE / Byte.SIZE; checkBounds(bytes, index, typeBytes); for (int i = 0; i < typeBytes; i++) { int b = bytes[index++]; b = (b < 0) ? b + 256 : b; int shiftBits = Byte.SIZE * (typeBytes - i - 1); val += (b << shiftBits); } return val; } /** * Utility method used to bounds check the byte array * and requested offset & length values. * * @param bytes * @param offset * @param length */ public static void checkBounds(byte[] bytes, int offset, int length) { if (length < 0) throw new IndexOutOfBoundsException("Index out of range: " + length); if (offset < 0) throw new IndexOutOfBoundsException("Index out of range: " + offset); if (offset > bytes.length - length) throw new IndexOutOfBoundsException("Index out of range: " + (offset + length)); } }