Java Data Type How to - Decode string in decimal, Hexidecimal and Octal to integer








Question

We would like to know how to decode string in decimal, Hexidecimal and Octal to integer.

Answer

// ww  w .  j  av  a  2 s .  c  om

public class Main {
  public static void main(String[] args) {
    String decimal = "10"; // Decimal
    String hexa = "0XFF"; // Hexa
    String octal = "077"; // Octal

    Integer number = Integer.decode(decimal);
    System.out.println("String [" + decimal + "] = " + number);

    number = Integer.decode(hexa);
    System.out.println("String [" + hexa + "] = " + number);

    number = Integer.decode(octal);
    System.out.println("String [" + octal + "] = " + number);
  }
}

The code above generates the following result.