有效大二进制字符串上的BigInteger NumberFormatException

时间:2012-12-06 22:39:35

标签: java string biginteger numberformatexception

所以,我使用BigInteger将一些二进制字符串转换为数字表示,最终陷入了一个奇怪的错误。

当这行代码运行时,它会引发NumberFormatException:

BigInteger temp = new BigInteger(strbuf.toString(), 2);

其中strbuf具有以下字符串(仅由零和1组成):

"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)

1 个答案:

答案 0 :(得分:0)

根据我对j2ee5的狩猎书,有一种尝试(隐含地 建议)将值转换为 java.lang.Integer ,一个包装器 int类型。那就是问题所在。注意异常中的第二个“at”行 放松...

这可能是一个优化问题..为您的主要优化关闭 程序不是简化的演示程序。

此致

ArrowInTree