ByteUtil { public static byte[] hexStringToBytes(String data) { if (data == null) return EMPTY_BYTE_ARRAY; if (data.startsWith("0x")) data = data.substring(2); if (data.length() % 2 == 1) data = "0" + data; return Hex.decode(data); }  static byte[] appendByte(byte[] bytes, byte b); static byte[] bigIntegerToBytes(BigInteger b, int numBytes); static byte[] bigIntegerToBytesSigned(BigInteger b, int numBytes); static byte[] bigIntegerToBytes(BigInteger value); static BigInteger bytesToBigInteger(byte[] bb); static int matchingNibbleLength(byte[] a, byte[] b); static byte[] longToBytes(long val); static byte[] longToBytesNoLeadZeroes(long val); static byte[] intToBytes(int val); static byte[] intToBytesNoLeadZeroes(int val); static String toHexString(byte[] data); static byte[] calcPacketLength(byte[] msg); static int byteArrayToInt(byte[] b); static long byteArrayToLong(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 int firstNonZeroByte(byte[] data); static byte[] stripLeadingZeroes(byte[] data); static boolean increment(byte[] bytes); static byte[] copyToArray(BigInteger value); static ByteArrayWrapper wrap(byte[] data); static byte[] setBit(byte[] data, int pos, int val); static int getBit(byte[] data, int pos); static byte[] and(byte[] b1, byte[] b2); static byte[] or(byte[] b1, byte[] b2); static byte[] xor(byte[] b1, byte[] b2); static byte[] xorAlignRight(byte[] b1, byte[] b2); static byte[] merge(byte[]... arrays); static boolean isNullOrZeroArray(byte[] array); static boolean isSingleZero(byte[] array); static Set<byte[]> difference(Set<byte[]> setA, Set<byte[]> setB); static int length(byte[]... bytes); static byte[] intsToBytes(int[] arr, boolean bigEndian); static int[] bytesToInts(byte[] arr, boolean bigEndian); static void bytesToInts(byte[] b, int[] arr, boolean bigEndian); static void intsToBytes(int[] arr, byte[] b, boolean bigEndian); static short bigEndianToShort(byte[] bs); static short bigEndianToShort(byte[] bs, int off); static byte[] shortToBytes(short n); static byte[] hexStringToBytes(String data); static byte[] hostToBytes(String ip); static String bytesToIp(byte[] bytesIp); static int numberOfLeadingZeros(byte[] bytes); static byte[] parseBytes(byte[] input, int offset, int len); static byte[] parseWord(byte[] input, int idx); static byte[] parseWord(byte[] input, int offset, int idx); static final byte[] EMPTY_BYTE_ARRAY; static final byte[] ZERO_BYTE_ARRAY; }
@Test public void testHexStringToBytes() { { String str = "0000"; byte[] actuals = ByteUtil.hexStringToBytes(str); byte[] expected = new byte[] {0, 0}; assertArrayEquals(expected, actuals); } { String str = "0x0000"; byte[] actuals = ByteUtil.hexStringToBytes(str); byte[] expected = new byte[] {0, 0}; assertArrayEquals(expected, actuals); } { String str = "0x45a6"; byte[] actuals = ByteUtil.hexStringToBytes(str); byte[] expected = new byte[] {69, -90}; assertArrayEquals(expected, actuals); } { String str = "1963093cee500c081443e1045c40264b670517af"; byte[] actuals = ByteUtil.hexStringToBytes(str); byte[] expected = Hex.decode(str); assertArrayEquals(expected, actuals); } { String str = "0x"; byte[] actuals = ByteUtil.hexStringToBytes(str); byte[] expected = new byte[] {}; assertArrayEquals(expected, actuals); } { String str = "0"; byte[] actuals = ByteUtil.hexStringToBytes(str); byte[] expected = new byte[] {0}; assertArrayEquals(expected, actuals); } { String str = "0x00"; byte[] actuals = ByteUtil.hexStringToBytes(str); byte[] expected = new byte[] {0}; assertArrayEquals(expected, actuals); } { String str = "0xd"; byte[] actuals = ByteUtil.hexStringToBytes(str); byte[] expected = new byte[] {13}; assertArrayEquals(expected, actuals); } { String str = "0xd0d"; byte[] actuals = ByteUtil.hexStringToBytes(str); byte[] expected = new byte[] {13, 13}; assertArrayEquals(expected, actuals); } }