Convert A Byte Array To An Int
Join the DZone community and get the full member experience.
Join For Free
/**
* Convert the byte array to an int.
*
* @param b The byte array
* @return The integer
*/
public static int byteArrayToInt(byte[] b) {
return byteArrayToInt(b, 0);
}
/**
* Convert the byte array to an int starting from the given offset.
*
* @param b The byte array
* @param offset The array offset
* @return The integer
*/
public static int byteArrayToInt(byte[] b, int offset) {
int value = 0;
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (b[i + offset] & 0x000000FF) << shift;
}
return value;
}
Data Types
Convert (command)
Opinions expressed by DZone contributors are their own.
Comments