Java hexadecimal to int conversion - need to remove 0X?

I have a file with many hexadecimal numbers (e.g. - 0X3B4). I am trying to parse this file how to assign these numbers to integers, but Integer.parseInt does not seem to work.

   int testint = Integer.parseInt("3B4",16);  <- WORKS

   int testint = Integer.parseInt("0X3B4",16);

gives an error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "0x3b4"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

What is the correct way to set the value 0XB4 to int?

I have to get rid of 0X - it's not uncommon to represent hexagons this way ...

+4
source share
1 answer

You can do

int hex = Integer.decode("0x3b4");

You are correct that parseInt and parseLong do not accept 0x or 0X

+8
source

All Articles