Bytes { public static byte[] toByteArray(int... uints) { byte[] bytez = new byte[uints.length]; for (int i = 0; i < uints.length; i++) { if (uints[i] > 255 || uints[i] < 0) throw new IllegalArgumentException("Cannot convert to byte. Number should be between 0 and (255) 0xff. " + "Number:" + uints[i]); bytez[i] = (byte) (uints[i] & 0xff); } return bytez; }  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; }
@Test public void testtoByteArray() { Assert.assertArrayEquals(Bytes.toByteArray(0x7e, 0xac, 0x8a, 0x93), ba); }
@Test(expected = IllegalArgumentException.class) public void testtoByteArrayNegativeException() { Assert.assertArrayEquals(Bytes.toByteArray(-1, 0xac, 0x8a, 0x93), ba); }
@Test(expected = IllegalArgumentException.class) public void testtoByteArrayLArgeNumberException() { Assert.assertArrayEquals(Bytes.toByteArray(256, 0xac, 0x8a, 0x93), ba); }
@Test public void testToByte() { byte[] baReverse = {(byte) 0x93, (byte) 0x8a, (byte) 0xac, 0x7e}; Assert.assertArrayEquals(Bytes.toByteArray(bigEndianInt, true), ba); Assert.assertArrayEquals(Bytes.toByteArray(bigEndianInt, false), baReverse); Assert.assertArrayEquals(Bytes.toByteArray(littleEndianInt, false), ba); Assert.assertArrayEquals(Bytes.toByteArray(littleEndianInt, true), baReverse); }
@Test public void testToByteShort() { byte[] baShort = {0x43, (byte) 0xac}; byte[] baShortReverse = {(byte) 0xac, 0x43}; short bigEndianShort = 0x43ac; short littleEndianShort = (short) 0xac43; Assert.assertArrayEquals(Bytes.toByteArray(bigEndianShort, true), baShort); Assert.assertArrayEquals(Bytes.toByteArray(bigEndianShort, false), baShortReverse); Assert.assertArrayEquals(Bytes.toByteArray(littleEndianShort, false), baShort); Assert.assertArrayEquals(Bytes.toByteArray(littleEndianShort, true), baShortReverse); }
@Test public void testToByteArrayShort() { byte[] baBe = {0x7e, (byte) 0xac, (byte) 0x8a, (byte) 0x93}; byte[] baLe = {(byte) 0xac, 0x7e, (byte) 0x93, (byte) 0x8a}; short[] sarr = {0x7eac, (short) 0x8a93}; Assert.assertArrayEquals(Bytes.toByteArray(sarr, sarr.length, true), baBe); Assert.assertArrayEquals(Bytes.toByteArray(sarr, sarr.length, false), baLe); }
@Test public void testByteArray() { Assert.assertArrayEquals(Bytes.toByteArray(0xCA, 0xFE, 0xBA, 0xBE, 0x45), new byte[]{(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE, 0x45}); }
@Test public void testToByteArrayInt() { int[] sarr4 = {0xCAFEBABE, 0xDEADBEEF}; int[] sarr3 = {0xCAFEBA, 0xDEADBE}; int[] sarr2 = {0xCAFE, 0xDEAD}; int[] sarr1 = {0xCA, 0xFE, 0xBA, 0xBE}; Assert.assertArrayEquals(Bytes.toByteArray(sarr4, sarr4.length, 4, true), Bytes.toByteArray(0xCA, 0xFE, 0xBA, 0xBE, 0xDE, 0xAD, 0xBE, 0xEF)); Assert.assertArrayEquals(Bytes.toByteArray(sarr4, sarr4.length, 4, false), Bytes.toByteArray(0xBE, 0xBA, 0xFE, 0xCA, 0xEF, 0xBE, 0xAD, 0xDE)); Assert.assertArrayEquals(Bytes.toByteArray(sarr3, sarr3.length, 3, true), Bytes.toByteArray(0xCA, 0xFE, 0xBA, 0xDE, 0xAD, 0xBE)); Assert.assertArrayEquals(Bytes.toByteArray(sarr3, sarr3.length, 3, false), Bytes.toByteArray(0xBA, 0xFE, 0xCA, 0xBE, 0xAD, 0xDE)); Assert.assertArrayEquals(Bytes.toByteArray(sarr2, sarr2.length, 2, true), Bytes.toByteArray(0xCA, 0xFE, 0xDE, 0xAD)); Assert.assertArrayEquals(Bytes.toByteArray(sarr2, sarr2.length, 2, false), Bytes.toByteArray(0xFE, 0xCA, 0xAD, 0xDE)); Assert.assertArrayEquals(Bytes.toByteArray(sarr1, sarr1.length, 1, true), Bytes.toByteArray(0xCA, 0xFE, 0xBA, 0xBE)); Assert.assertArrayEquals(Bytes.toByteArray(sarr1, sarr1.length, 1, false), Bytes.toByteArray(0xCA, 0xFE, 0xBA, 0xBE)); }