DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Convert A Byte Array To An Int
/**
* 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;
}






Comments
Snippets Manager replied on Mon, 2010/03/08 - 3:44pm