将二进制转换为十进制并翻转0和1 JAVA

时间:2017-06-18 10:37:15

标签: java binary parseint

<p id=userName>{{userDetails._source.name}}</p>

所以res = 00000000000000000000000000101110 并且res2 = 11111111111111111111111111010001

但是当我尝试将其转换回int时,它会显示在控制台中:

    public static int reverseIntBitwise(int value) {
    String res = String.format("%32s", 
    Integer.toBinaryString(value)).replace(' ', '0');

    char[] charArray = res.toCharArray();
    for(int i=0; i<charArray.length; i++){
        if(charArray[i] == '0'){
            charArray[i]='1';
        }else if(charArray[i]=='1'){
            charArray[i]='0';
        }
    }
    String res2 = new String(charArray);
    return Integer.parseInt(res2, 2);
}

任何人都可以帮忙解决这个问题吗? 感谢

2 个答案:

答案 0 :(得分:3)

您收到var options = { title: 'Statistics', backgroundColor: 'red', backgroundColor: { stroke: 'green', strokeWidth: 5 }, }; 因为结果号码NumberFormatException超出了4294967249的范围(即超过Integer)。您可以尝试使用Integer.MAX_VALUE,例如:

BigInteger

答案 1 :(得分:0)

二进制11111111111111111111111111010001的十进制数将溢出整数数据类型。

使用长。

相关问题