There are such lines:

0x16 012 0b10 62 

Need to translate them into:

 22 10 2 62 

accordingly, but when using Integer.parseInt(String, int) I get a NumberFormatException , how to solve this problem?

  • @pavlofff, not really. - Nofate

2 answers 2

for example, you can write such a function

 private static int StringToInteger(String input) { if(input.startsWith("0x")) { return Integer.parseInt(input.substring(2), 16); } else if(input.startsWith("0b")) { return Integer.parseInt(input.substring(2), 2); } else if(input.startsWith("0") && input.length() > 1) { return Integer.parseInt(input.substring(1), 8); } else { return Integer.parseInt(input); } } 
  • Thanks for the example! Now I understand the work of the parseInt method much better. - Evgeniy

The Integer.parseInt(String, int) method is passed a string containing a number in one or another number system without any prefixes :

 System.out.println(Integer.parseInt("16", 16)); // 22 System.out.println(Integer.parseInt("12", 8)); // 10 System.out.println(Integer.parseInt("10", 2)); // 2 System.out.println(Integer.parseInt("62", 10)); // 62