Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/org/apache/commons/lang3/Conversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,10 @@ public static boolean[] hexDigitToBinary(final char hexChar) {
* @throws IllegalArgumentException if {@code hexDigit} is not a hexadecimal digit.
*/
public static int hexDigitToInt(final char hexChar) {
final int digit = Character.digit(hexChar, 16);
if (digit < 0) {
if (!CharUtils.isHex(hexChar)) {
throw new IllegalArgumentException("Cannot convert '" + hexChar + "' to a hexadecimal digit");
}
return digit;
return Character.digit(hexChar, 16);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/apache/commons/lang3/ConversionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,11 @@ void testHexDigitToInt() {
assertEquals(15, Conversion.hexDigitToInt('F'));
assertEquals(15, Conversion.hexDigitToInt('f'));
assertIllegalArgumentException(() -> Conversion.hexDigitToInt('G'));
assertIllegalArgumentException(() -> Conversion.hexDigitToInt('\uFF41')); // FULLWIDTH LATIN SMALL LETTER A
assertIllegalArgumentException(() -> Conversion.hexDigitToInt('\uFF26')); // FULLWIDTH LATIN CAPITAL LETTER F
assertIllegalArgumentException(() -> Conversion.hexDigitToInt('\uFF19')); // FULLWIDTH DIGIT NINE
assertIllegalArgumentException(() -> Conversion.hexDigitToInt('\u0669')); // ARABIC-INDIC DIGIT NINE
assertIllegalArgumentException(() -> Conversion.hexDigitToInt('\u096B')); // DEVANAGARI DIGIT FIVE
}

/**
Expand Down Expand Up @@ -723,6 +728,7 @@ void testHexToInt() {
assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToInt(src, Integer.MIN_VALUE, 0, 0, 1));
assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.hexToInt(src, Integer.MAX_VALUE, 0, 0, 1));
assertIllegalArgumentException(() -> Conversion.hexToInt(src, Integer.MAX_VALUE, 0, 0, Integer.SIZE));
assertIllegalArgumentException(() -> Conversion.hexToInt("\uFF41\uFF41", 0, 0, 0, 2));
}

/**
Expand Down
Loading