Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store hex value (0x45E213) in an integer

In my application I used a converter to create from 3 values > RGB-colors an Hex value. I use this to set my gradient background in my application during runtime.

Now is this the following problem. The result of the converter is a (String) #45E213, and this can't be stored in an integer. But when you create an integer,

int hex = 0x45E213;

it does work properly, and this doesn't give errors.

Now I knew of this, I Replaced the # to 0x, and tried it to convert from String to Integer.

int hexToInt = new Integer("0x45E213").intValue();

But now I get the numberFormatException, because while converting, it will not agree with the character E?

How can I solve this? Because I really need it as an Integer or Java/Eclipse won't use it in its method.

like image 355
Dion Segijn Avatar asked Mar 17 '12 11:03

Dion Segijn


1 Answers

http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html

The Integer constructor with a string behaves the same as parseInt with radix 10. You presumably want String.parseInt with radix 16.

Integer.parseInt("45E213", 16)

or to cut off the 0x

Integer.parseInt("0x45E213".substring(2), 16);

or

Integer.parseInt("0x45E213".replace("0x",""), 16);
like image 192
Matt Esch Avatar answered Oct 03 '22 19:10

Matt Esch