Java tutorial
//package com.java2s; public class Main { /** * Convert a one or two byte array of bytes to an unsigned two-byte integer. * * <p><b>NOTE:</b> This function mostly exists as an example of how to use * twoBytesToUnsignedInt(). It is unlikely the use case it embodies * will occur often.</p> * * @param ba * @return */ public static int byteArrayToUnsignedInt(byte[] byteArray) { if (byteArray.length > Integer.SIZE) throw new IllegalArgumentException("Array length must match one of the following types:\n Byte==" + Byte.SIZE + ", Short==" + Short.SIZE + ", Integer==" + Integer.SIZE); return (int) byteArrayToUnsignedLong(byteArray); } /** * @param byteArray * @return */ public static long byteArrayToUnsignedLong(byte[] byteArray) { int length; long value = 0; if (byteArray.length == Byte.SIZE / Byte.SIZE) { length = Byte.SIZE / Byte.SIZE; } else if (byteArray.length == Short.SIZE / Byte.SIZE) { length = Short.SIZE / Byte.SIZE; } else if (byteArray.length == Integer.SIZE / Byte.SIZE) { length = Integer.SIZE / Byte.SIZE; } else if (byteArray.length == Long.SIZE / Byte.SIZE) { length = Long.SIZE / Byte.SIZE; } else throw new IllegalArgumentException("Array length must match one of the following types:\n Byte==" + Byte.SIZE + ", Short==" + Short.SIZE + ", Integer==" + Integer.SIZE + ", Long==" + Long.SIZE); for (int i = 0; i < length; i++) { value |= ((0xffL & byteArray[i]) << (8 * (length - i - 1))); } return value; } }