I have hexadecimal String eg. "0x103E" , I want to convert it into integer.
Means String no = "0x103E";
to int hexNo = 0x103E;
I tried Integer.parseInt("0x103E",16); but it gives number format exception.
How do I achieve this ?
2 Answers
You just need to leave out the "0x" part (since it's not actually part of the number).
You also need to make sure that the number you are parsing actually fits into an integer which is 4 bytes long in Java, so it needs to be shorter than 8 digits as a hex number.
2 Comments
user3153014
It's giving decimal representation of the number. I want hexadecimal int.
mhlz
Integers are always binary. Converting them to a String again (even at the point where you're looking at it with your debugger) requires some base. The default for that is base 10. If you want to output the integer in base 16 again you need to use
Integer.toString(i, 16) (as described here: docs.oracle.com/javase/8/docs/api/java/lang/… )
0x. In that other question, the issue was the size of the number being converted.