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

时间:2020-11-30 16:48:08

So, I was using BigInteger to convert some binary string to a numeric representation and ended up getting stuck on a weird error.

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

When this line is code runs, it raises a NumberFormatException:

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

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

where strbuf has the following string (made up only of zeros and ones):

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

"1001110000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010"

At first I thought perhaps the string or value were too big, but the following standalone java class compiles and runs just fine:

起初我想也许字符串或值太大了,但是下面的独立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);
    }
}

Is there something Im missing here? Why does the same code with apparently same values fail to run on my main application?

我在这里遗失了什么?为什么具有明显相同值的相同代码无法在我的主应用程序上运行?

The exception message:

异常消息:

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 个解决方案

#1


0  

According to my safari book for j2ee5, there is an attempt (implicitly suggested) to convert the value to java.lang.Integer, a wrapper for the int type. That is the problem. Note the 2nd "at" line in the exception unwind...

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

This may be an optimization problem..turn optimization off for your main program not the cut-down demo program.

这可能是一个优化问题..为主程序而不是缩减演示程序进行优化。

Sincerely,

ArrowInTree

#1


0  

According to my safari book for j2ee5, there is an attempt (implicitly suggested) to convert the value to java.lang.Integer, a wrapper for the int type. That is the problem. Note the 2nd "at" line in the exception unwind...

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

This may be an optimization problem..turn optimization off for your main program not the cut-down demo program.

这可能是一个优化问题..为主程序而不是缩减演示程序进行优化。

Sincerely,

ArrowInTree