LANG-747 NumberUtils does not handle Long Hex numbers
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1384126 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e826656..b19877f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -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>
diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
index eb5259e..c5cc948 100644
--- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
+++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
@@ -418,8 +418,13 @@
/**
* <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 @@
* <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 @@
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);