4

所以,我使用 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)
4

1 回答 1

0

根据我为 j2ee5 编写的 safari book,尝试(隐式建议)将值转换为java.lang.Integer,这是 int 类型的包装器。那就是问题所在。请注意异常展开中的第二个“at”行...

这可能是一个优化问题。关闭主程序的优化,而不是精简的演示程序。

真挚地,

箭树

于 2012-12-06T22:56:22.497 回答