Author: sebb
Date: Wed Sep 12 21:10:18 2012
New Revision: 1384126

URL: http://svn.apache.org/viewvc?rev=1384126&view=rev
Log:
LANG-747 NumberUtils does not handle Long Hex numbers

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1384126&r1=1384125&r2=1384126&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml Wed Sep 12 21:10:18 2012
@@ -39,6 +39,7 @@
     <action issue="LANG-765" 
type="fix">EventListenerSupport.ProxyInvocationHandler no longer defines 
serialVersionUID</action>
     <action issue="LANG-764" type="fix">StrBuilder is now serializable</action>
     <action issue="LANG-761" type="fix">Fix Javadoc Ant warnings</action>
+    <action issue="LANG-747" type="fix">NumberUtils does not handle Long Hex 
numbers</action>
     <action issue="LANG-743" type="fix">Javadoc bug in static inner class 
DateIterator</action>
     <action issue="LANG-675" type="add">Add Triple class (ternary version of 
Pair)</action>
     <action issue="LANG-462" type="add">FastDateFormat supports parse 
methods</action>

Modified: 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java?rev=1384126&r1=1384125&r2=1384126&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
 (original)
+++ 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
 Wed Sep 12 21:10:18 2012
@@ -418,8 +418,13 @@ public class NumberUtils {
     /**
      * <p>Turns a string value into a java.lang.Number.</p>
      *
-     * <p>First, the value is examined for a type qualifier on the end
-     * (<code>'f','F','d','D','l','L'</code>).  If it is found, it starts 
+     * <p>If the string starts with <code>0x</code> or <code>-0x</code> (lower 
or upper case), it
+     * will be interpreted as a hexadecimal integer - or long, if the number 
of digits after the 0x
+     * prefix is more than 8.
+     * Values with leading <code>0</code>'s will not be interpreted as 
octal.</p>
+     *
+     * <p>Then, the value is examined for a type qualifier on the end, i.e. 
one of
+     * <code>'f','F','d','D','l','L'</code>.  If it is found, it starts 
      * trying to create successively larger types from the type specified
      * until one is found that can represent the value.</p>
      *
@@ -428,10 +433,6 @@ public class NumberUtils {
      * <code>BigInteger</code> and from <code>Float</code> to
      * <code>BigDecimal</code>.</p>
      *
-     * <p>If the string starts with <code>0x</code> or <code>-0x</code> (lower 
or upper case), it
-     * will be interpreted as a hexadecimal integer.  Values with leading
-     * <code>0</code>'s will not be interpreted as octal.</p>
-     *
      * <p>Returns <code>null</code> if the string is <code>null</code>.</p>
      *
      * <p>This method does not trim the input string, i.e., strings with 
leading
@@ -456,6 +457,13 @@ public class NumberUtils {
             return null;
         }
         if (str.startsWith("0x") || str.startsWith("-0x") || 
str.startsWith("0X") || str.startsWith("-0X")) {
+            int hexDigits = str.length() - 2; // drop 0x
+            if (str.startsWith("-")) { // drop -
+                hexDigits--;
+            }
+            if (hexDigits > 8) { // too many for an int
+                return createLong(str);
+            }
             return createInteger(str);
         }   
         char lastChar = str.charAt(str.length() - 1);


Reply via email to