HexString { @Nonnull public static byte[] getHexByteArray(@Nonnull final String hexValue) { assert hexValue != null; assert !hexValue.isEmpty(); final String hex; if ((hexValue.length() % 2) == 0) { hex = hexValue; } else { hex = '0' + hexValue; } assert hex.length() >= 2; int i = hex.length() - 1; int j = (hex.length() / 2) - 1; final byte[] bytes = new byte[j + 1]; while (i > 0) { final int lo = getHexDigitValue(hex.charAt(i)); final int hi = getHexDigitValue(hex.charAt(i - 1)); final int b = (hi * 16) + lo; assert ((0 <= b) && (b <= 255)); bytes[j] = (byte) b; i = i - 2; j = j - 1; } return bytes; } private  HexString(); @Nonnull static String getHexString(@Nonnull final String textValue); @Nonnull static String getHexString(@Nonnull final byte[] bytes); @Nonnull static byte[] getHexByteArray(@Nonnull final String hexValue);  }
@SuppressWarnings("NumericCastThatLosesPrecision") @Test public void testHexByteArray() { LOG.info("testHexByteArray"); Assert.assertArrayEquals(new byte[]{(byte) 0}, HexString.getHexByteArray("0")); Assert.assertArrayEquals(new byte[]{(byte) 1}, HexString.getHexByteArray("1")); Assert.assertArrayEquals(new byte[]{(byte) 2}, HexString.getHexByteArray("2")); Assert.assertArrayEquals(new byte[]{(byte) 3}, HexString.getHexByteArray("3")); Assert.assertArrayEquals(new byte[]{(byte) 4}, HexString.getHexByteArray("4")); Assert.assertArrayEquals(new byte[]{(byte) 5}, HexString.getHexByteArray("5")); Assert.assertArrayEquals(new byte[]{(byte) 6}, HexString.getHexByteArray("6")); Assert.assertArrayEquals(new byte[]{(byte) 7}, HexString.getHexByteArray("7")); Assert.assertArrayEquals(new byte[]{(byte) 8}, HexString.getHexByteArray("8")); Assert.assertArrayEquals(new byte[]{(byte) 9}, HexString.getHexByteArray("9")); Assert.assertArrayEquals(new byte[]{(byte) 10}, HexString.getHexByteArray("a")); Assert.assertArrayEquals(new byte[]{(byte) 11}, HexString.getHexByteArray("b")); Assert.assertArrayEquals(new byte[]{(byte) 12}, HexString.getHexByteArray("c")); Assert.assertArrayEquals(new byte[]{(byte) 13}, HexString.getHexByteArray("d")); Assert.assertArrayEquals(new byte[]{(byte) 14}, HexString.getHexByteArray("e")); Assert.assertArrayEquals(new byte[]{(byte) 15}, HexString.getHexByteArray("f")); Assert.assertArrayEquals(new byte[]{(byte) 10}, HexString.getHexByteArray("0A")); Assert.assertArrayEquals(new byte[]{(byte) 10}, HexString.getHexByteArray("0a")); Assert.assertArrayEquals(new byte[]{(byte) 0}, HexString.getHexByteArray("00")); Assert.assertArrayEquals(new byte[]{(byte) 1}, HexString.getHexByteArray("01")); Assert.assertArrayEquals(new byte[]{(byte) 2}, HexString.getHexByteArray("02")); Assert.assertArrayEquals(new byte[]{(byte) 3}, HexString.getHexByteArray("03")); Assert.assertArrayEquals(new byte[]{(byte) 4}, HexString.getHexByteArray("04")); Assert.assertArrayEquals(new byte[]{(byte) 5}, HexString.getHexByteArray("05")); Assert.assertArrayEquals(new byte[]{(byte) 6}, HexString.getHexByteArray("06")); Assert.assertArrayEquals(new byte[]{(byte) 7}, HexString.getHexByteArray("07")); Assert.assertArrayEquals(new byte[]{(byte) 8}, HexString.getHexByteArray("08")); Assert.assertArrayEquals(new byte[]{(byte) 9}, HexString.getHexByteArray("09")); Assert.assertArrayEquals(new byte[]{(byte) 10}, HexString.getHexByteArray("0a")); Assert.assertArrayEquals(new byte[]{(byte) 11}, HexString.getHexByteArray("0b")); Assert.assertArrayEquals(new byte[]{(byte) 12}, HexString.getHexByteArray("0c")); Assert.assertArrayEquals(new byte[]{(byte) 13}, HexString.getHexByteArray("0d")); Assert.assertArrayEquals(new byte[]{(byte) 14}, HexString.getHexByteArray("0e")); Assert.assertArrayEquals(new byte[]{(byte) 15}, HexString.getHexByteArray("0f")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 0}, HexString.getHexByteArray("000")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 1}, HexString.getHexByteArray("001")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 2}, HexString.getHexByteArray("002")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 3}, HexString.getHexByteArray("003")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 4}, HexString.getHexByteArray("004")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 5}, HexString.getHexByteArray("005")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 6}, HexString.getHexByteArray("006")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 7}, HexString.getHexByteArray("007")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 8}, HexString.getHexByteArray("008")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 9}, HexString.getHexByteArray("009")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 10}, HexString.getHexByteArray("00a")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 11}, HexString.getHexByteArray("00b")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 12}, HexString.getHexByteArray("00c")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 13}, HexString.getHexByteArray("00d")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 14}, HexString.getHexByteArray("00e")); Assert.assertArrayEquals(new byte[]{(byte) 0, (byte) 15}, HexString.getHexByteArray("00f")); Assert.assertArrayEquals(new byte[]{(byte) 10}, HexString.getHexByteArray("0A")); Assert.assertArrayEquals(new byte[]{(byte) 10}, HexString.getHexByteArray("a")); Assert.assertArrayEquals(new byte[]{(byte) 255}, HexString.getHexByteArray("ff")); Assert.assertArrayEquals(new byte[]{(byte) 65}, HexString.getHexByteArray("41")); Assert.assertArrayEquals(new byte[]{(byte) 65, (byte) 66}, HexString.getHexByteArray("4142")); }