HexEncoder { public static byte[] getBytes(final String hexString) { if (hexString.length() % 2 != 0) throw new IllegalHexStringException("The length of your hex string is " + hexString.length() + ". Odd-length is not allowed."); byte[] b = new byte[hexString.length() / 2]; for (int i = 0; i < b.length; i++) { int index = i * 2; int v = Integer.parseInt(hexString.substring(index, index + 2), 16); b[i] = (byte) v; } return b; }  static byte[] getBytes(final String hexString); static String getString(final byte[] bytes);  }
@Test(expected = IllegalHexStringException.class) public void failure_GetBytes_from_odd_hex_string() { String hex = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1"; byte[] result = HexEncoder.getBytes(hex); }