从 Integer.parseInt() 获取 "NumberFormatException",可能是由于误解

Getting "NumberFormatException" from Integer.parseInt(), possibly from misinterpretation

我只是想知道为什么我的代码给我一个 NumberFormatException?

从 stack overflow 的其他帖子来看,人们似乎对空格有问题,但我的似乎根本没有这个问题。

package suop.space;

import java.util.BitSet;

public class ColourUtils {
    public static short getAlpha(final int rgb) {
        return (short) ((rgb >> 24) & 0xff);
    }

    public static short getRed(final int rgb) {
        return (short) ((rgb & 0x00ff0000) >> 16);
    }

    public static short getGreen(final int rgb) {
        return (short) ((rgb & 0x0000ff00) >> 8);
    }

    public static short getBlue(final int rgb) {
        return (short) (rgb & 0x000000ff);
    }

    public static int getRGB(int alpha, int red, int green, int blue) {

        BitSet rgbValueBitSet = new BitSet(32);

        addIntToBitSet(alpha, rgbValueBitSet, 0);
        addIntToBitSet(red, rgbValueBitSet, 9);
        addIntToBitSet(green, rgbValueBitSet, 17);
        addIntToBitSet(blue, rgbValueBitSet, 25);


        System.out.println("rgbValueBitSet: " + rgbValueBitSet);

        StringBuilder rgbBinaryStringBuilder = new StringBuilder();
        for (int i = 0; i < rgbValueBitSet.length(); i++) {
            if (rgbValueBitSet.get(i)) {
                rgbBinaryStringBuilder.append(1);
            } else {
                rgbBinaryStringBuilder.append(0);
            }
        }

        System.out.println(rgbBinaryStringBuilder.toString());

        int rgbValue = Integer.parseInt(rgbBinaryStringBuilder.toString(), 2);
        System.out.println(rgbValue);

        return rgbValue;
    }

    private static void addIntToBitSet(int inputInt, BitSet inputBitSet, int byteToStartFrom) {

        char[] inputCharArray = Integer.toBinaryString(inputInt).toCharArray();
        int[] inputIntArray = new int[inputCharArray.length];

        for (int i = 0; i < inputCharArray.length; i++) {
            inputIntArray[i] = Character.getNumericValue(inputCharArray[i]);
        }

        for (int i = 0; i < inputIntArray.length; i++) {
            if (inputIntArray[i] >= 1) {
                inputBitSet.set(i + byteToStartFrom);
            } else {
                inputBitSet.clear(i + byteToStartFrom);
            }
        }
    }
}

尝试 运行 程序时出现此错误:

rgbValueBitSet: {0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 12, 13, 14, 15, 17, 18, 20, 21, 22, 23, 25, 26, 28, 29, 30, 31}
11111111011011110110111101101111
Exception in thread "main" java.lang.NumberFormatException: For input string: "11111111011011110110111101101111" under radix 2
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:660)
    at suop.space.ColourUtils.getRGB(ColourUtils.java:45)
    at suop.space.EffectsUtils.divideByTwo(EffectsUtils.java:19)
    at suop.space.Main.main(Main.java:38)

Process finished with exit code 1

11111111011011110110111101101111 是我试图通过 Integer.parseInt() 解析的内容。

我猜它可能试图使 11111111011011110110111101101111 本身成为一个 int,它比整数大得多。问题是告诉它我认为的基数会让它把它识别为二进制,虽然我不确定。

您有一个表示 32 位无符号值的字符串。 'int' 是一个 32 位有符号值,即字符串值中没有符号,输入不能超过 31 位。

负数的内部表示设置了高位并不意味着您可以在该数字的字符串表示中显式设置高位。

您与尝试

的情况相同
  parseInt("4285493103", 10);

在 Java 8 之后,有 parseUnsignedInt,在我看来它可以满足您的要求。我个人无法保证这一点,因为我从来不需要使用它。

否则,使用parseLong。

by telling it the radix I thought would make it identify it as binary,

它被解析为二进制:这意味着它仅以 2 为底。基数不会改变负值的表示形式,也不会改变允许的绝对范围。


也许这个小测试程序可以帮助您了解发生了什么?

class N {
    static public void main(String... a) {
        System.out.println(Integer.parseUnsignedInt("11111111011011110110111101101111", 2));
    }
}