HexUtil { static public int hexCharToIntValue(char c) { if (c == '0') { return 0; } else if (c == '1') { return 1; } else if (c == '2') { return 2; } else if (c == '3') { return 3; } else if (c == '4') { return 4; } else if (c == '5') { return 5; } else if (c == '6') { return 6; } else if (c == '7') { return 7; } else if (c == '8') { return 8; } else if (c == '9') { return 9; } else if (c == 'A' || c == 'a') { return 10; } else if (c == 'B' || c == 'b') { return 11; } else if (c == 'C' || c == 'c') { return 12; } else if (c == 'D' || c == 'd') { return 13; } else if (c == 'E' || c == 'e') { return 14; } else if (c == 'F' || c == 'f') { return 15; } else { throw new IllegalArgumentException("The character [" + c + "] does not represent a valid hex digit"); } }  static String toHexString(byte[] bytes); static String toHexString(byte[] bytes, int offset, int length); static void appendHexString(StringBuilder buffer, byte[] bytes); static void appendHexString(StringBuilder buffer, byte[] bytes, int offset, int length); static String toHexString(byte value); static void appendHexString(StringBuilder buffer, byte value); static String toHexString(short value); static void appendHexString(StringBuilder buffer, short value); static String toHexString(int value); static void appendHexString(StringBuilder buffer, int value); static String toHexString(long value); static void appendHexString(StringBuilder buffer, long value); static int hexCharToIntValue(char c); static byte[] toByteArray(CharSequence hexString); static byte[] toByteArray(CharSequence hexString, int offset, int length); static char[] HEX_TABLE; }
@Test public void hexCharToIntValue() throws Exception { Assert.assertEquals(0, HexUtil.hexCharToIntValue('0')); Assert.assertEquals(1, HexUtil.hexCharToIntValue('1')); Assert.assertEquals(2, HexUtil.hexCharToIntValue('2')); Assert.assertEquals(3, HexUtil.hexCharToIntValue('3')); Assert.assertEquals(4, HexUtil.hexCharToIntValue('4')); Assert.assertEquals(5, HexUtil.hexCharToIntValue('5')); Assert.assertEquals(6, HexUtil.hexCharToIntValue('6')); Assert.assertEquals(7, HexUtil.hexCharToIntValue('7')); Assert.assertEquals(8, HexUtil.hexCharToIntValue('8')); Assert.assertEquals(9, HexUtil.hexCharToIntValue('9')); Assert.assertEquals(10, HexUtil.hexCharToIntValue('A')); Assert.assertEquals(11, HexUtil.hexCharToIntValue('B')); Assert.assertEquals(12, HexUtil.hexCharToIntValue('C')); Assert.assertEquals(13, HexUtil.hexCharToIntValue('D')); Assert.assertEquals(14, HexUtil.hexCharToIntValue('E')); Assert.assertEquals(15, HexUtil.hexCharToIntValue('F')); Assert.assertEquals(10, HexUtil.hexCharToIntValue('a')); Assert.assertEquals(11, HexUtil.hexCharToIntValue('b')); Assert.assertEquals(12, HexUtil.hexCharToIntValue('c')); Assert.assertEquals(13, HexUtil.hexCharToIntValue('d')); Assert.assertEquals(14, HexUtil.hexCharToIntValue('e')); Assert.assertEquals(15, HexUtil.hexCharToIntValue('f')); try { Assert.assertEquals(15, HexUtil.hexCharToIntValue('g')); Assert.fail(); } catch (IllegalArgumentException e) { } }