ByteArrayUtil { public static byte[] fromHex(String hex) { hex = HEX_IGNORED_CHARS.matcher(hex).replaceAll("").toUpperCase(); if (hex.length() % 2 != 0) { throw new IllegalArgumentException("Odd numbered hex array"); } byte[] byteArray = new byte[hex.length() / 2]; for (int i = 0; i < hex.length(); i += 2) { byteArray[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16)); } return byteArray; } private  ByteArrayUtil(); static byte[] fromHex(String hex); static String toHex(byte[] byteArray); static int twoBytesToInt(byte[] bytes, int offset); static int twoBytesSignedToInt(byte[] bytes, int offset); static int threeBytesToInt(byte[] bytes, int offset); static int threeBytesSignedToInt(byte[] bytes, int offset); static int fourBytesToInt(byte[] bytes, int offset);  }
@Test(expected = NullPointerException.class) public void fromHex_null() { byte[] bytes = ByteArrayUtil.fromHex(null); }
@Test public void fromHex_empty() { byte[] bytes = ByteArrayUtil.fromHex(""); assertThat(bytes.length).isEqualTo(0); }
@Test(expected = IllegalArgumentException.class) public void fromHex_odd_length() { byte[] bytes = ByteArrayUtil.fromHex(HEXSTRING_ODD); }
@Test public void fromHex_good_hex() { byte[] bytes = ByteArrayUtil.fromHex(HEXSTRING_GOOD); assertThat(bytes).isEqualTo(BYTEARRAY_GOOD); }