2

I am trying to convert to int like this, but I am getting an exception.

    String strHexNumber = "0x1";
    int decimalNumber = Integer.parseInt(strHexNumber, 16);
    Exception in thread "main" java.lang.NumberFormatException: For input string: "0x1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:458)

It would be a great help if someone can fix it.

Thanks.

7 Answers 7

5

That's because the 0x prefix is not allowed. It's only a Java language thing.

String strHexNumber = "F777";
int decimalNumber = Integer.parseInt(strHexNumber, 16);
System.out.println(decimalNumber);

If you want to parse strings with leading 0x then use the .decode methods available on Integer, Long etc.

int value = Integer.decode("0x12AF");
System.out.println(value);
Sign up to request clarification or add additional context in comments.

Comments

4

Sure - you need to get rid of the "0x" part if you want to use parseInt:

int parsed = Integer.parseInt("100", 16);
System.out.println(parsed); // 256

If you know your value will start with "0x" you can just use:

String prefixStripped = hexNumber.substring(2);

Otherwise, just test for it:

number = number.startsWith("0x") ? number.substring(2) : number;

Note that you should think about how negative numbers will be represented too.

EDIT: Adam's solution using decode will certainly work, but if you already know the radix then IMO it's clearer to state it explicitly than to have it inferred - particularly if it would surprise people for "035" to be treated as octal, for example. Each method is appropriate at different times, of course, so it's worth knowing about both. Pick whichever one handles your particular situation most cleanly and clearly.

1 Comment

Not necessary. See my solution.
1

Integer.parseInt can only parse strings that are formatted to look just like an int. So you can parse "0" or "12343" or "-56" but not "0x1".

Comments

0

You need to strip off the 0x from the front of the string before you ask the Integer class to parse it. The parseInt method expects the string passed in to be only numbers/letters of the specified radix.

Comments

0

try using this code here:-

import java.io.*;
 import java.lang.*;

public class  HexaToInteger{

   public static void main(String[] args) throws IOException{

   BufferedReader read = 

   new BufferedReader(new InputStreamReader(System.in));

   System.out.println("Enter the hexadecimal value:!");

   String s = read.readLine();

   int i = Integer.valueOf(s, 16).intValue();

   System.out.println("Integer:=" + i);

   }

 }

2 Comments

well then the number format is wrong, cuz java dont recognize his 0x1 numericals.
The question that was asked was how to convert strings with 0x prefixes... not those without...
0

Yeah, Integer is still expecting some kind of String of numbers. that x is really going to mess things up.

Comments

0

Depending on the size of the hex, you may need to use a BigInteger (you can probably skip the "L" check and trim in yours ;-) ):

        // Convert HEX to decimal
        if (category.startsWith("0X") && category.endsWith("L")) {
            category = new BigInteger(category.substring(2, category.length() - 1), 16).toString();
        } else if (category.startsWith("0X")) {
            category = new BigInteger(category.substring(2, category.length()), 16).toString();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.