i have code that looks like this
public static void main(String[] args) {
String string= "11011100010000010001000000000000";
String string1= "00000000010000110000100000101100";
System.out.println(Integer.toHexString(Integer.parseInt(string1,2)));
System.out.println(Integer.toHexString(Integer.parseInt(string,2)));
}
the first string convert just fine but the second one has an error of java.lang.NumberFormatException
dont know what the problem is
When the most significant bit of a 32-character binary number is set to
1, the resultant value exceeds the range of positive numbers supported byint, and can no longer be interpreted as a valid integer number. This causes the exception according to the documentation:An exception of type NumberFormatException is thrown if any of the following situations occurs:
In order to enter this negative binary value, use
-sign in front of your number, and convert the remaining bits to 2-s complement representation.If you need numbers that are longer than 32 bits, or if you would like the value to continue being interpreted as a positive number, you would need to switch to the 64-bit integer data type.