【发布时间】:2015-11-28 15:18:39
【问题描述】:
我需要将一个大的二进制字符串(长度 = 251)转换为 Long 类型的变量,以执行一些逻辑运算符,例如(AND、OR 等),但是由于字符串的长度,此操作中会出现错误,和我想的一样!
我该如何解决这个问题!?
我的代码:
System.out.println("value: " + v2.length()); // printing 251
Long decimalValue1 = Long.parseLong(v1, 2); //Error here appear.
if(type == "AND")
{
eq = decimalValue1 & decimalValue2;
}
else if(type == "OR")
{
eq = decimalValue1 | decimalValue2;
}
错误: For input string: "01000000000000000001000000000010000000010100010001000000100000011101101010010000000001101110001001111101001001000111001000001101101011111000000010000000010010111100010101010011111011011101101111111100"
【问题讨论】:
-
A
long只有 64 位。您不能将这么大的二进制文件放入long。如果您想对无限位数进行逻辑运算,请考虑使用BitSet类。 -
大概是
NumberFormatException。你应该学会使用e.printStackTrace();(然后是正确的日志)而不是打印出e.getMessage();。否则错误消息没有多大意义。 -
或者使用 BigInteger。
标签: java binary type-conversion long-integer logical-operators