United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 7006142 Error parsing String representing signed binary number (2-complement)
7006142 : Error parsing String representing signed binary number (2-complement)

Details
Type:
Bug
Submit Date:
2010-12-10
Status:
Closed
Updated Date:
2012-03-28
Project Name:
JDK
Resolved Date:
2012-03-28
Component:
core-libs
OS:
windows_7
Sub-Component:
java.lang
CPU:
x86
Priority:
P4
Resolution:
Duplicate
Affected Versions:
6u22
Fixed Versions:

Related Reports
Duplicate:

Sub Tasks

Description
FULL PRODUCT VERSION :
JDK 1.6_18

ADDITIONAL OS VERSION INFORMATION :
Windows 7

A DESCRIPTION OF THE PROBLEM :
int i=Integer.parseInt(Integer.toBinaryString(-1), 2);
ought to return -1, but


Exception in thread "main" java.lang.NumberFormatException: For input string: "11111111111111111111111111111111" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
	at java.lang.Integer.parseInt(Integer.java:461)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Problem occurs every time you try to parse a string with the sign-bit set

Byte.parseByte("11111111",2) fails because it calls Integer.ParseInt and therafter chechs the value againt -128 and -127


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
	public static void main(String[] args) {
		int num=-1;
		String binString=Integer.toBinaryString(num);
		System.out.println(binString);
		//result: 11111111111111111111111111111111 (32 bits)
		// now try to reverse
		num=Integer.parseInt(binString, 2);
		//and we crashes
		// java.lang.NumberFormatException:
		//For input string: "11111111111111111111111111111111"
	}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
		if (binString.charAt(0)=='1')
			binString=binString.substring(1);
		num=Integer.parseInt(binString, 2)-Integer.MIN_VALUE;

                                    

Comments
EVALUATION

The output of Integer.toBinaryString is unsigned; read it back in using Integer.parseUnsignedInt.

Closing as duplicate of 4215269.
                                     
2012-03-28



Hardware and Software, Engineered to Work Together