# How to convert a String into an Hexadecimal ?

• 02-15-2010, 09:28 AM
ze snow
How to convert a String into an Hexadecimal ?
Hi!

I have a String which represents a hexadecimal number (e.g. "5FA2"). Ideally, I would like to convert this String into an int (e.g. 0x5FA2), is there a quick way to do this ?

I can't use the method "Integer.parseInt()" since my String represents an hexadecimal and not a decimal.

• 02-15-2010, 09:35 AM
r035198x
Read the API specs for the Integer class. There is another Integer.parseInt method that you will find interesting.
• 02-15-2010, 09:36 AM
JosAH
Quote:

Originally Posted by ze snow
Hi!

I have a String which represents a hexadecimal number (e.g. "5FA2"). Ideally, I would like to convert this String into an int (e.g. 0x5FA2), is there a quick way to do this ?

I can't use the method "Integer.parseInt()" since my String represents an hexadecimal and not a decimal.

You shouldn't have stopped reading the API documentation after you've found a method that doesn't apply. There's also an overloaded method that can do the job.

kind regards,

Jos
• 02-15-2010, 01:33 PM
ze snow
Thanks guys!

Looks like the method Integer.decode() is working :-)
• 02-15-2010, 02:50 PM
JosAH
Quote:

Originally Posted by ze snow
Thanks guys!

Looks like the method Integer.decode() is working :-)

decode( ... ) gives you an Integer, not an int; auto(un)boxing will help you but still an additional Integer object was created (ignoring the fact of caching some Integers here). Better use the overloaded parse( ... ) method. Read the API documentation for the Integer class (again).

kind regards,

Jos
• 02-15-2010, 03:42 PM
ze snow
Sorry, I just missed this other parse method.
Indeed, that works even better, saves 1-2 lines :-)

Thanks again JosAH!
• 02-15-2010, 06:09 PM
r035198x
Quote:

Originally Posted by ze snow
Sorry, I just missed this other parse method.
...
Thanks again JosAH!