Parsing a hex string into an integer?

Hi,

What's the correct way to parse a hex integer string?:

    String str = "0xFF222222";
 
    int val = Integer.parseInt(str, 16);

The above throws a number format exception.

Thanks
DJ_AM_JuiceboxAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
for_yanConnect With a Mentor Commented:

That is exactly your number:

 String hex = "FF222222";
                    long num = Long.parseLong(hex,16);
      System.out.println("num: " + num);

num: 4280427042
0
 
for_yanCommented:
You should remove 0x part
0
 
for_yanCommented:

            String hex = "FF22222";
                    long num = Long.parseLong(hex,16);
      System.out.println("num: " + num);

output:
num: 267526690

Open in new window

0
 
objectsCommented:
When parsing a hex string you need to remove the 0x from the string as for_yan has mentioned above.
The 0x is not part of the actual hex value you are parsing.
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.

All Courses

From novice to tech pro — start learning today.