HexConverter { public static byte[] convertFromHex(final char[] toConvert) { if (toConvert.length % 2 != 0) { throw new IllegalArgumentException("The supplied character array must contain an even number of hex chars."); } byte[] response = new byte[toConvert.length / 2]; for (int i = 0; i < response.length; i++) { int posOne = i * 2; response[i] = (byte) (toByte(toConvert, posOne) << 4 | toByte(toConvert, posOne + 1)); } return response; }  static String convertToHexString(byte[] toBeConverted); static byte[] convertToHexBytes(byte[] toBeConverted); static byte[] convertFromHex(final char[] toConvert); static byte[] convertFromHex(final String toConvert);  }
@Test public void shouldConvertHexString() { byte[] bytes = HexConverter.convertFromHex("00010A0B0F106364657F8081FF"); assertThat(bytes).isEqualTo(new byte[]{ 0, 1, 10, 11, 15, 16, 99, 100, 101, 127, -128, -127, -1 }); }
@Test(expected = IllegalArgumentException.class) public void shouldRejectStringOfWrongLength() { HexConverter.convertFromHex("1"); }
@Test(expected = IllegalArgumentException.class) public void shouldRejectNonHexCharacter() { HexConverter.convertFromHex("GG"); }