Java Data Type How to - Convert Decimal to Octal back and forth








Question

We would like to know how to convert Decimal to Octal back and forth.

Answer

 public class Main {
// www  .j  a  v  a2  s  .  c  o m
  public static void main(String[] args) {
    int integer = 1024;

    String octal = Integer.toOctalString(integer);

    System.out.printf("Octal value of %d is '%s'.\n", integer, octal);
    System.out.printf("Octal value of %1$d is '%1$o'.\n", integer);

    int original = Integer.parseInt(octal, 8);
    System.out.printf("Integer value of octal '%s' is %d.", octal, original);
  }
}

The code above generates the following result.