Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse hex color String to Integer

I'm working on some code in Robolectric, namely IntegerResourceLoader. The following method is throwing a RuntimeException when rawValue is something such as 0xFFFF0000:

@Override
public Object convertRawValue( String rawValue ) {
    try {
        return Integer.parseInt( rawValue );
    } catch ( NumberFormatException nfe ) {
        throw new RuntimeException( rawValue + " is not an integer." );
    }
}

I tried using Integer.decode(String) but that throws a NumberFormatException even though the grammar appears to be correct.

like image 686
Christopher Perry Avatar asked Dec 20 '22 16:12

Christopher Perry


1 Answers

decode() is the right method to call but it fails because 0xFFFF0000 is higher than 0x7fffffff max limit for integer. You may want to consider Long.

like image 68
Alex Gitelman Avatar answered Dec 24 '22 02:12

Alex Gitelman