简体   繁体   English

NumberFormatExcpetion 同时使用 Long.parseLong() 将 base-2 数字(作为字符串)解析为 base-10 Long int

[英]NumberFormatExcpetion while using Long.parseLong() to parse a base-2 number(as a string) to a base-10 Long int

So I'm trying to flip the bits of a long int, this is how I'm doing it but I'm getting the NumberFormatException.所以我试图翻转一个 long int 的位,这就是我正在做的事情,但我得到了 NumberFormatException。 I'm converting it to a base-2 string and add zeros to the left to become a 32 char, then flip the bits, then convert it back to long of base-10.我将其转换为以 2 为底的字符串并在左侧添加零以成为 32 字符,然后翻转位,然后将其转换回以 10 为底的长整数。

Long n =4L;
String bits = String.format("%32s", Long.toBinaryString(n)).replace(' ', '0');
bits = bits.replace("0", "3");
bits = bits.replace("1", "0");
bits = bits.replace("3", "1");
return Long.parseLong(bits.trim(), 10);

4L after converting to base 2:转换为基数 2 后的 4L:

00000000000000000000000000000100 000000000000000000000000000000100

after flipping all bits:, I'm getting this:翻转所有位后:,我得到了这个:

Exception in thread "main" java.lang.NumberFormatException: For input string: "11111111111111111111111111111011"

What's wrong?怎么了? I checked for non-printable chars but there aren't, also I trimmed the number in case there are any extra spaces, I tried to add L at the end but nothing worked, what's the problem?我检查了不可打印的字符但没有,我还修剪了数字以防有多余的空格,我试图在最后添加 L 但没有任何效果,有什么问题?

I am not 100% sure if this is what you are looking for.我不是 100% 确定这是否是您正在寻找的。 This will convert a string of binary to a long.这会将二进制字符串转换为长字符串。 Note: I am not checking for 0's in the method.注意:我没有在方法中检查 0。 You may need to change that to suit your needs.您可能需要更改它以满足您的需要。

    public long stringBinaryToLong( final String binaryString) {
        if(binaryString == null) {
            return 0l;
        }
        // Check length for long on binary string. How many binary digits are the max length.
        // Determine this by taking log(base 2) for Long max.
        // log base 2 not available so use this technique. --> log_base2 (x) = log_base10 (x) / log_base10 (2)
        if( binaryString.length() > Math.ceil( Math.log10( (double) Long.MAX_VALUE ) / Math.log10( 2.0 ) ) ) {
            throw new IllegalStateException("string of binary digits exceeds max size for a Java long.");
        }
        double base = 2.0, power=0.0;
        long result = 0l;
        for(int index=binaryString.length() - 1; index > -1; index--) {
            if( binaryString.charAt(index) == '1') {
                result += Math.pow(base,  power);
            }
            power += 1.0;
        }
        return result;
    }

So the problem was with the radix parameter of the parseLong method, radix is to specify the base of the string that I want to parse, not the output of the parse method所以问题出在 parseLong 方法的 radix 参数上,radix 是指定我要解析的字符串的基数,而不是 parse 方法的 output

So the whole issue was solved by substituting this:所以整个问题通过替换这个来解决:

return Long.parseLong(bits.trim(), 10);

with this:有了这个:

return Long.parseLong(bits.trim(), 2);

now, the string of bits (the input of parseLong method) is of base 2 as mentioned in the radix parameter现在,位串(parseLong 方法的输入)的基数为 2,如 radix 参数中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM