Octal To Decimal Conversion In Java

Sharing is healthy !!

Today we are going to learn octal to decimal conversion in java.

The conversion from octal to decimal takes place with the help of Integer wrapper class.

This class provides a method Integer.parseInt(String s, int radix) which converts binary, hexadecimal and octal number to decimal number.

The parameters in the above method namely “String s” and “int radix” indicates

  • String s – binary, hexadecimal and octal number,
  • int radix – in what base the first argument is represented.

The method Integer.parseInt(String s, int radix) size is less than 32-bits. However, the size greater than 32-bits will throw an unchecked runtime java.lang.NumberFormatException.

Now let’s see an example on how to convert octal to decimal in java,

example: Octal To Decimal Conversion In Java

Also Read – Convert Decimal to Octal Java Program

import java.util.Scanner;

public class OctalToDecimal
{
   int number;

   void getValue()
   {
      System.out.println("Octal To Decimal Conversion In Java");
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number for conversion: ");
      number = Integer.parseInt(sc.nextLine(), 8);
   }

   void convertValue()
   {
      String decimal = Integer.toString(number);
      System.out.println("Converted Decimal value is: " + decimal);
   }

   public static void main(String[] args)
   {
      OctalToDecimal otd = new OctalToDecimal();
      otd.getValue();
      otd.convertValue();
   }
}

Output:

Octal To Decimal Conversion In Java

Sharing is healthy !!