所以,我使用 BigInteger 将一些二进制字符串转换为数字表示,最终陷入了一个奇怪的错误。
当这一行代码运行时,它会引发 NumberFormatException:
BigInteger temp = new BigInteger(strbuf.toString(), 2);
其中 strbuf 具有以下字符串(仅由零和一组成):
"1001110000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010"
起初我以为字符串或值可能太大了,但以下独立的 java 类编译并运行得很好:
import java.math.BigInteger;
class test {
public static void main(String[] argv) {
StringBuffer strbuf = new StringBuffer("1001110000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010");
BigInteger big = new BigInteger(strbuf.toString(), 2);
System.out.println(big);
}
}
我在这里缺少什么吗?为什么具有明显相同值的相同代码无法在我的主应用程序上运行?
异常消息:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1001110000000100"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.math.BigInteger.<init>(Unknown Source)
at org.app.star.pad(star.pad:42)