ByteUtil { public static byte[] stripLeadingZeroes(byte[] data) { if (data == null) return null; int firstNonZero = 0; int i = 0; for (; i < data.length; ++i) { if (data[i] != 0) { firstNonZero = i; break; } } if (i == data.length) return new byte[1]; if (firstNonZero == 0) return data; byte[] result = new byte[data.length - firstNonZero]; System.arraycopy(data, firstNonZero, result, 0, data.length - firstNonZero); return result; }  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 testStripLeadingZeroes() { byte[] test1 = new byte[] { 0x00, 0x01 }; byte[] test2 = new byte[] { 0x00, 0x00, 0x01 }; byte[] expected = new byte[] { 0x01 }; assertArrayEquals(expected, ByteUtil.stripLeadingZeroes(test1)); assertArrayEquals(expected, ByteUtil.stripLeadingZeroes(test2)); }