JDK-5096564 : new methods in Byte, Short, Integer, and Long to properly handle negatives
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-09-03
  • Updated: 2004-09-03
  • Resolved: 2004-09-03
Related Reports
Duplicate :  
Description

Name: rmT116609			Date: 09/02/2004


A DESCRIPTION OF THE REQUEST :
Byte.parseByte("FF", 16) throws a  NumberFormatException.  This seems rather illogical since "FF" is a perfectly respectable byte value of -1.  The behavior of parseByte, parseShort, parseInt, and parseLong is such that a leading minus sign is required when parsing negative numbers.  So parseByte("-1", 16) works but parseByte("FF", 16) does not.

A handy enhancement would be to overload parseByte etc.  with something like this:

byte parseByte(String s, int radix, boolean acceptTwosCompliment)

So parseByte("FF", 16) or  parseByte("FF", 16, false) would throw NumberFormatException, completely preserving backward compatability, but parseByte("FF", 16, true) would return -1.  Of course there would be similar enhancements to Short, Integer, and Long.

JUSTIFICATION :
The API should provide a simple way to, e.g, parse string "FF" into a byte.  Currently, one would have to call parseInt and manipulate the result into a byte.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
As mentioned above:

parseByte("FF", 16, true) should return -1 and
parseByte("FF", 16) should continue to throw NumberFormatException.

CUSTOMER SUBMITTED WORKAROUND :
Use parseInt and examine the result.
(Incident Review ID: 301049) 
======================================================================

Comments
EVALUATION The submitter is asking for the ability to parse hexadecimal strings as unsigned values, a dup of 4215269. ###@###.### 2004-09-03
03-09-2004