ByteUtil { public static byte[] calcPacketLength(byte[] msg) { int msgLen = msg.length; byte[] len = { (byte)((msgLen >> 24) & 0xFF), (byte)((msgLen >> 16) & 0xFF), (byte)((msgLen >> 8) & 0xFF), (byte)((msgLen ) & 0xFF)}; return len; }  static byte[] appendByte(byte[] bytes, byte b); static byte[] bigIntegerToBytes(BigInteger b, int numBytes); static byte[] bigIntegerToBytes(BigInteger value); static int matchingNibbleLength(byte[] a, byte[] b); static byte[] longToBytes(long val); static String toHexString(byte[] data); static byte[] calcPacketLength(byte[] msg); static int byteArrayToInt(byte[] b); static String nibblesToPrettyString(byte[] nibbles); static String oneByteToHexString(byte value); static int numBytes(String val); static byte[] encodeValFor32Bits(Object arg); static byte[] encodeDataList(Object... args); static byte[] stripLeadingZeroes(byte[] data); static boolean increment(byte[] bytes); static byte[] copyToArray(BigInteger value); static final byte[] EMPTY_BYTE_ARRAY; }
@Test public void testCalcPacketLength() { byte[] test = new byte[] { 0x0f, 0x10, 0x43 }; byte[] expected = new byte[] { 0x00, 0x00, 0x00, 0x03 }; assertArrayEquals(expected, ByteUtil.calcPacketLength(test)); }