@Test public void testToInt() { assertEquals(Bytes.toInt(ba, true), bigEndianInt); assertEquals(Bytes.toInt(ba, false), littleEndianInt); assertEquals(Bytes.toInt((byte) 0x7e, (byte) 0xac, (byte) 0x8a, (byte) 0x93, true), bigEndianInt); assertEquals(Bytes.toInt((byte) 0x7e, (byte) 0xac, (byte) 0x8a, (byte) 0x93, false), littleEndianInt); assertEquals(Bytes.toInt(new byte[]{0x7e, (byte) 0xac, (byte) 0x8a}, true), 0x7eac8a); assertEquals(Bytes.toInt(new byte[]{0x7e, (byte) 0xac, (byte) 0x8a}, false), 0x8aac7e); assertEquals(Bytes.toInt(new byte[]{0x7e, (byte) 0xac}, true), 0x7eac); assertEquals(Bytes.toInt(new byte[]{0x7e, (byte) 0xac}, false), 0xac7e); assertEquals(Bytes.toInt(new byte[]{0x7e}, true), 0x7e); assertEquals(Bytes.toInt(new byte[]{0x7e}, false), 0x7e); assertEquals(Bytes.toInt(new byte[]{0x2f, (byte) 0xff}, false), 0xff2f); }
public static int toInt(byte[] pb, boolean bigEndian) { switch (pb.length) { case 1: return pb[0] & 0xff; case 2: if (bigEndian) { return (pb[0] << 8 & 0xff00) | (pb[1] & 0xff); } else { return (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } case 3: if (bigEndian) { return (pb[0] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[2] & 0xff); } else { return (pb[2] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } case 4: if (bigEndian) { return (pb[0] << 24 & 0xff000000) | (pb[1] << 16 & 0xff0000) | (pb[2] << 8 & 0xff00) | (pb[3] & 0xff); } else { return (pb[3] << 24 & 0xff000000) | (pb[2] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } default: throw new IllegalArgumentException("1,2,3 or 4 byte arrays allowed. size:" + pb.length); } }
Bytes { public static int toInt(byte[] pb, boolean bigEndian) { switch (pb.length) { case 1: return pb[0] & 0xff; case 2: if (bigEndian) { return (pb[0] << 8 & 0xff00) | (pb[1] & 0xff); } else { return (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } case 3: if (bigEndian) { return (pb[0] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[2] & 0xff); } else { return (pb[2] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } case 4: if (bigEndian) { return (pb[0] << 24 & 0xff000000) | (pb[1] << 16 & 0xff0000) | (pb[2] << 8 & 0xff00) | (pb[3] & 0xff); } else { return (pb[3] << 24 & 0xff000000) | (pb[2] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } default: throw new IllegalArgumentException("1,2,3 or 4 byte arrays allowed. size:" + pb.length); } } }
Bytes { public static int toInt(byte[] pb, boolean bigEndian) { switch (pb.length) { case 1: return pb[0] & 0xff; case 2: if (bigEndian) { return (pb[0] << 8 & 0xff00) | (pb[1] & 0xff); } else { return (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } case 3: if (bigEndian) { return (pb[0] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[2] & 0xff); } else { return (pb[2] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } case 4: if (bigEndian) { return (pb[0] << 24 & 0xff000000) | (pb[1] << 16 & 0xff0000) | (pb[2] << 8 & 0xff00) | (pb[3] & 0xff); } else { return (pb[3] << 24 & 0xff000000) | (pb[2] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } default: throw new IllegalArgumentException("1,2,3 or 4 byte arrays allowed. size:" + pb.length); } }  }
Bytes { public static int toInt(byte[] pb, boolean bigEndian) { switch (pb.length) { case 1: return pb[0] & 0xff; case 2: if (bigEndian) { return (pb[0] << 8 & 0xff00) | (pb[1] & 0xff); } else { return (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } case 3: if (bigEndian) { return (pb[0] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[2] & 0xff); } else { return (pb[2] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } case 4: if (bigEndian) { return (pb[0] << 24 & 0xff000000) | (pb[1] << 16 & 0xff0000) | (pb[2] << 8 & 0xff00) | (pb[3] & 0xff); } else { return (pb[3] << 24 & 0xff000000) | (pb[2] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } default: throw new IllegalArgumentException("1,2,3 or 4 byte arrays allowed. size:" + pb.length); } }  static byte[] toByteArray(int... uints); static int toInt(byte[] pb, boolean bigEndian); static int normalize(int i, int bitCount); static void normalize(int iarr[], int bitCount); static byte[] toByteArray(int i, int size, boolean isBigEndian); static int toInt(byte b0, byte b1, byte b2, byte b3, boolean bigEndian); static byte[] toByteArray(int i, boolean bigEndian); static byte[] toByteArray(short i, boolean bigEndian); static int[] toIntArray(byte[] ba, int amount, boolean bigEndian); static int[] toIntArray(byte[] ba, final int amount, final int bytePerInteger,
      boolean bigEndian); static int[] toReducedBitIntArray(
      byte[] ba,
      final int amount,
      int bytePerInteger,
      int bitAmount,
      boolean bigEndian); static short[] toShortArray(byte[] ba, int amount, boolean bigEndian); static byte[] toByteArray(short[] sa, int amount, boolean bigEndian); static byte[] toByteArray(int[] ia, int amount, int bytePerInteger, boolean bigEndian); static String toHexWithZeros(byte b); static String toHex(byte b); static String toHex(byte[] bytes); static String toHexWithZeros(byte[] bytes); static void hexDump(OutputStream os, byte[] bytes, int columns); static void hexDump(byte[] bytes, int columns); }
Bytes { public static int toInt(byte[] pb, boolean bigEndian) { switch (pb.length) { case 1: return pb[0] & 0xff; case 2: if (bigEndian) { return (pb[0] << 8 & 0xff00) | (pb[1] & 0xff); } else { return (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } case 3: if (bigEndian) { return (pb[0] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[2] & 0xff); } else { return (pb[2] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } case 4: if (bigEndian) { return (pb[0] << 24 & 0xff000000) | (pb[1] << 16 & 0xff0000) | (pb[2] << 8 & 0xff00) | (pb[3] & 0xff); } else { return (pb[3] << 24 & 0xff000000) | (pb[2] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[0] & 0xff); } default: throw new IllegalArgumentException("1,2,3 or 4 byte arrays allowed. size:" + pb.length); } }  static byte[] toByteArray(int... uints); static int toInt(byte[] pb, boolean bigEndian); static int normalize(int i, int bitCount); static void normalize(int iarr[], int bitCount); static byte[] toByteArray(int i, int size, boolean isBigEndian); static int toInt(byte b0, byte b1, byte b2, byte b3, boolean bigEndian); static byte[] toByteArray(int i, boolean bigEndian); static byte[] toByteArray(short i, boolean bigEndian); static int[] toIntArray(byte[] ba, int amount, boolean bigEndian); static int[] toIntArray(byte[] ba, final int amount, final int bytePerInteger,
      boolean bigEndian); static int[] toReducedBitIntArray(
      byte[] ba,
      final int amount,
      int bytePerInteger,
      int bitAmount,
      boolean bigEndian); static short[] toShortArray(byte[] ba, int amount, boolean bigEndian); static byte[] toByteArray(short[] sa, int amount, boolean bigEndian); static byte[] toByteArray(int[] ia, int amount, int bytePerInteger, boolean bigEndian); static String toHexWithZeros(byte b); static String toHex(byte b); static String toHex(byte[] bytes); static String toHexWithZeros(byte[] bytes); static void hexDump(OutputStream os, byte[] bytes, int columns); static void hexDump(byte[] bytes, int columns); static final byte[] EMPTY_BYTE_ARRAY; }