StringUtils { public static byte[] toByteArray(@Nonnull String hex) { if ((hex.length() % 2) != 0) { hex = "0" + hex; } byte[] ret = new byte[hex.length() / 2]; for (int i = 0; i < ret.length; i++) { ret[i] = (byte) Integer.parseInt(hex.substring(i*2, (i*2) + 2), 16); } return ret; }  static byte[] toByteArray(@Nonnull String hex); static byte[] toByteArray(@Nonnull String hex, boolean removeWhitespace); static boolean isNullOrEmpty(@Nullable String value); @Nonnull static String nullToEmpty(@Nullable String s); @Nullable static String emptyToNull(@Nullable String s);  }
@Test public void testToByteArray() { String hex = "00112233"; String hex2 = "00 11 22 33"; byte[] array = StringUtils.toByteArray(hex); byte[] expected = new byte[] { 0x00, 0x11, 0x22, 0x33 }; assertEquals(expected, array); array = StringUtils.toByteArray(hex2, true); expected = new byte[] { 0x00, 0x11, 0x22, 0x33 }; assertEquals(expected, array); array = StringUtils.toByteArray(hex, false); expected = new byte[] { 0x00, 0x11, 0x22, 0x33 }; assertEquals(expected, array); }