Link to home
Start Free TrialLog in
Avatar of anguslai
anguslai

asked on

Best way to convert Hex String to int?

Best way to convert Hex String to int?
eg. "FF" -> 255
P.S. Must work for Java 1.0
ASKER CERTIFIED SOLUTION
Avatar of jasbro
jasbro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or you can use the decode(String nm) method in Integer.  You would have to prepend a '0x' to the beginning of the string (after the optional '-').  So:

if (hexString.charAt(0) == '-') value = Integer.decode("-0x" + hexString.substring(1));
else value = Integer.decode(hexString);

you will have to put this in a try catch block to catch number format exceptions....
Avatar of stev75
stev75

thanks!
But it's like this:
value=(value*16)
 
here is the best way to do it

String hex = ffabcc;

int value = Integer.parsInt(hex,16);

Thanks and Regards
Y. Kamesh Rao