# 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
LVL 2
###### Who is Participating?

Commented:
int digitVal(char c){
if (c=='0') return 0;
if (c=='1') return 1;
if (c=='2') return 2;
...
if (c=='A') return 10;
...
if (c=='F') return 15;
else return 0;  //may want to return a negative, or throw exception
}

int parseHex(String hexString){
int value=0;
for(i = 0; i<hexString.length; i++){
value=(value*8) + digitVal(hexString.charAt(i);
}
return value;
}

There also may be a class that does this, I will look around
0

Commented:
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....
0

Commented:
thanks!
But it's like this:
value=(value*16)

0

Commented:
here is the best way to do it

String hex = ffabcc;

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

Thanks and Regards
Y. Kamesh Rao
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.