HexUtils { public static byte[] hexToBytes(String hexString, String delimiter) { String ucHexString = hexString.toUpperCase(); final String[] splitted = ucHexString.split(delimiter); final byte[] bytes = new byte[splitted.length]; int pos = 0; for (final String part : splitted) { final byte[] in = part.getBytes(StandardCharsets.UTF_8); if (in.length != 2) { throw new IllegalArgumentException("hexString needs to have an even length: " + hexString); } bytes[pos++] = (byte) (unhex(in[0]) << 4 | unhex(in[1])); } return bytes; } private  HexUtils(); static String bytesToHex(byte[] bytes, @Nullable CharSequence delimiter); static String bytesToHex(byte[] bytes); static byte[] byteToHex(final byte value); static byte[] hexToBytes(String hexString, String delimiter); static byte[] hexToBytes(String hexString); static byte hexToByte(byte high, byte low);  }
@Test public void testHexToBytesNoParams() { byte[] result = HexUtils.hexToBytes("41424344"); assertEquals("ABCD", new String(result)); }
@Test public void testHexToBytesWithDelimiter() { byte[] result = HexUtils.hexToBytes("41 42 43 44", " "); assertEquals("ABCD", new String(result)); }
@Test public void testHexToBytesWithMultiCharDelimiter() { byte[] result = HexUtils.hexToBytes("41-:-42-:-43-:-44", "-:-"); assertEquals("ABCD", new String(result)); }