You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2021/03/04 00:13:18 UTC

[GitHub] [commons-lang] aherbert commented on a change in pull request #727: LANG-1644 - Check if number is hexadecimal

aherbert commented on a change in pull request #727:
URL: https://github.com/apache/commons-lang/pull/727#discussion_r586910342



##########
File path: src/main/java/org/apache/commons/lang3/math/NumberUtils.java
##########
@@ -1715,6 +1715,50 @@ public static boolean isCreatable(final String str) {
         return !allowSigns && foundDigit;
     }
 
+    /**
+     * Checks whether the given String is a hex number.
+     *
+     * <p>Valid parameter include hexadecimal marked with the {@code 0x} or
+     * {@code 0X} qualifier.</p>
+     *
+     * <p>{@code Null} and empty String will return {@code false}.</p>
+     *
+     * <pre>
+     * NumberUtils.isHexNumber(null))                 = false
+     * NumberUtils.isHexNumber(""))                   = false
+     * NumberUtils.isHexNumber("0x12345678"))         = true
+     * NumberUtils.isHexNumber("0x7fffffffffffffff")) = true
+     * NumberUtils.isHexNumber("0x7FFFFFFFFFFFFFFF")) = true
+     * NumberUtils.isHexNumber("5D0"))                = true
+     * NumberUtils.isHexNumber("0x"))                 = false
+     * </pre>
+     *
+     * @param str the String to check.
+     * @return {@code true} if the string is a hex number.
+     * @since 3.12.1
+     */
+    public static boolean isHexNumber(final String str) {
+        if (StringUtils.isEmpty(str)) {
+            return false;
+        }
+        final char[] chars = str.toCharArray();
+        final int length = chars.length;
+        int i = 1;
+        if (chars[i] == 'x' || chars[i] == 'X') {

Review comment:
       If chars is length 1 then this will throw an index out of bounds exception.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org