Reputation: 23
I'm working with BufferedImage
(in PNG) and want to replace a colour with another.
I have all colours stored as strings for easy handling but...
for(int x=0;x<output.getWidth();x++)
for(int y=0;y<output.getHeight();y++)
if(output.getRGB(x,y)==Integer.parseInt("ffff00fe",16))
output.setRGB(x,y,Integer.parseInt("ffaaaaaa",16));
the resultant integers should be negative numbers, but it throws NumberFormatException
when I do output.getRGB(x,y)
it returns negative numbers on non-transparent pixels
Upvotes: 0
Views: 187
Reputation: 16359
Values greater that 0x7fff_ffff
are too large to be handled as signed ints.
Java 8 has added methods for dealing with ints
as if they contained unsigned values. Simply replace parseInt
with parseUnsignedInt:
Integer.parseUnsignedInt("ffaaaaaa", 16)
If you need to work with Java 7 and earlier, you can parse it as a long
and then cast it to int.
Or, if the values are constants, you can write them as numeric constants such as 0xffaaaaaa
or even 0xffaa_aaaa
and avoid dealing with string conversions (the underscores in numbers are allowed since Java 7 and can make them much easier to read).
Upvotes: 0
Reputation: 3537
The number 2,147,483,647 (or hexadecimal 7FFFFFFF) is the maximum positive value for a 32-bit signed binary integer. What you're trying to convert is something almost double that, which means the first bit of the binary number is a 1; in a signed binary integer the first bit being a 1 means it's a negative number.
Basically, you need something bigger to parse it.
Try (int) Long.parseLong("ffff00fe", 16)
instead of Integer.parseInt("ffff00fe",16)
Upvotes: 0