StringUtils { @NotNull public static byte[] convertHexToBytes(@NotNull String s) { checkNotNull(s); int len = s.length(); checkArgument(len % 2 == 0); len /= 2; byte[] buff = new byte[len]; for (int i = 0; i < len; i++) { buff[i] = (byte) ((getHexDigit(s, i + i) << 4) | getHexDigit(s, i + i + 1)); } return buff; } private  StringUtils(); @NotNull static String convertBytesToHex(@NotNull byte[] value); @NotNull static byte[] convertHexToBytes(@NotNull String s); static int estimateMemoryUsage(String s);  }
@Test public void testHexToBytes() { IOUtilsTest.assertEquals(new byte[] {(byte) 0xfa, (byte) 0xce}, StringUtils.convertHexToBytes("face")); IOUtilsTest.assertEquals(new byte[] {(byte) 0xfa, (byte) 0xce}, StringUtils.convertHexToBytes("fAcE")); IOUtilsTest.assertEquals(new byte[] {(byte) 0xfa, (byte) 0xce}, StringUtils.convertHexToBytes("FaCe")); IOUtilsTest.assertEquals(new byte[] {(byte) 0x09, (byte) 0xaf}, StringUtils.convertHexToBytes("09af")); IOUtilsTest.assertEquals(new byte[] {}, StringUtils.convertHexToBytes("")); }
@Test public void testInvalidHexToBytes() { for (String s : new String[]{"120", "1/", "9:", "fast", "a`", "ag", "0@", "aG"}) { try { StringUtils.convertHexToBytes(s); fail(); } catch (IllegalArgumentException expected) { } } }
@Test(expected = NullPointerException.class) public void testNullToBytes() { StringUtils.convertHexToBytes(null); }