[ 
https://issues.apache.org/jira/browse/LANG-1644?focusedWorklogId=560985&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-560985
 ]

ASF GitHub Bot logged work on LANG-1644:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 04/Mar/21 14:49
            Start Date: 04/Mar/21 14:49
    Worklog Time Spent: 10m 
      Work Description: arturobernalg commented on a change in pull request 
#727:
URL: https://github.com/apache/commons-lang/pull/727#discussion_r587531376



##########
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') {
+            i = 2;
+            if (i == length) {
+                return false;
+            }
+        }
+        for (; i < chars.length; i++) {

Review comment:
       Hi @garydgregory 
   I've trying use Range and i think it's more clean check the one by one the 
char. With range should look like:
   
   ```
   
    final Range<Integer> range = Range.between(0, 15);
   
           for (; pfxLen < chars.length; pfxLen++) {
               if (!range.contains(Character.getNumericValue(chars[pfxLen]))) {
                   return false;
               }
           }
   
   ```
   




----------------------------------------------------------------
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:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 560985)
    Time Spent: 2h 50m  (was: 2h 40m)

> Check if number is hexadecimal
> ------------------------------
>
>                 Key: LANG-1644
>                 URL: https://issues.apache.org/jira/browse/LANG-1644
>             Project: Commons Lang
>          Issue Type: Improvement
>            Reporter: Arturo Bernal
>            Priority: Minor
>          Time Spent: 2h 50m
>  Remaining Estimate: 0h
>
> IMO would be fine have a method that given a String tell you if is a valid 
> hexadecimal number instead of try to create the number. should be valid:
>  
>  * 5D0
>  * 0x7FFFFFFFFFFFFFFF
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to