-1

I'm trying to convert "0x16" and "0b10" to decimal using "Integer.parseInt("0x16", 10)", but I keep getting this error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "0x16"
  • The [documentation](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String,%20int%29) says that is correct to show that error. – Jongware Aug 27 '14 at 08:39
  • You shouldn't have the hex / binary indicator, and your base is wrong. See: http://stackoverflow.com/questions/15735079/convert-from-one-base-to-another-in-java – Evan Knowles Aug 27 '14 at 08:39

1 Answers1

3

First argumet of Integer.parseint function is a string which express a number, and second argumet is a base (or radix).

"0x16" is made of two parts, "0x" shows that the base is 16 and "16" shows the number in hexadecimal.

If you want to get integer value of "0x16", the code will be

int aValue;
aValue = Integer.parseint("16",16);

and then, you can show the value in decimal by following code

printf("%d" , aValue);

Points are 1. Binary, decimal and hexadecimal are formats to express integer values by strings. 2. "0x16" and "0b10" are combination of 'base' and 'value'(in string). 3. To convert combination string into integer, use Inter.parseint with 'string' as first argument and 'base' as second argument. (0x means 'base is 16', 0b means 'base is 2' etc.) Ex: "0x16" : parseint("16",16); "0b10" : parseint("10",2);

Fumu 7
  • 1,091
  • 1
  • 7
  • 8