Let’s take Byte.parseByte() as an example as one of the wrappers’ parseXXX().
From parseByte(String s, int radix)'s JavaDoc:
Parses the string argument as a signed byte in the radix specified by
the second argument.
But that’s not quite true if radix = 2. In other words, the binary literal of -127 is 10000000:
byte b = (byte) 0b10000000;
So the following should be true:
byte b = Byte.parseByte("10000000", 2);
but unfortunately, it throws NumberFormatException, and instead I have to do it as follows:
byte b = Byte.parseByte("-111111", 2);
where parseByte() parses the binary string as a sign-magnitude (the sign and the magnitude), where it should parse as a signed binary (2’s complement, i.e. MSB is the sign-bit).
Am I wrong about this?
Yes. The Javadoc says nothing about 2’s-complement. Indeed, it explicitly states how it recognises negative values (i.e. a
-prefix, so effectively “human-readable” sign-magnitude).Think about it another way. If
parseByteinterpreted radix-2 as 2’s-complement, what would you want it to do for radix-10 (or indeed, any other radix)? For consistency, it would have to be 10’s-complement, which would be inconvenient, I can assure you!