线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“11000001101000000000000000000000”
# Include <stdio.h>
# include <stdlib.h>
public static void main(String[] args) {
int bits = Integer.parseInt("1000001101000000000000… 2);
float f1 = Float.intBitsToFloat(bits);
int Sign = ((bits >> 31) == 0) ? 1 : -1;
int Exponent = ((bits >> 23) & 0xff);
int Mantissa = (Exponent== 0)
? (bits & 0x7fffff) << 1
: (bits & 0x7fffff) | 0x800000;
System.out.println("Sign: " + Sign + " Exponent: " + Exponent + "Mantissa:" + Mantissa);
System.out.println(f1);
}
答案 0 :(得分:1)
来自Integer.#parseInt(java.lang.String, int):
...字符串中的字符必须都是指定基数的数字(由Character.digit(char,int)返回非负值确定)...
不幸的是你想要解析负值。您可以尝试使用Long.parseLong
,然后将返回的long
转换为int
int bits = (int) Long.parseLong("11000001101000000000000000000000", 2);
这样你就可以获得具有相同字节表示的int
System.out.println(">"+Integer.toBinaryString(bits));
输出:
>11000001101000000000000000000000