Java convert octal to decimal and vice versa

Let’s learn java convert octal to decimal.

Java convert octal to decimal

To convert octal to decimal in java parseInt() method of Integer class is used. Syntax for Integer.parseInt() method is as follows,

public static int parseInt(String s, int radix) throws NumberFormatException

java convert octal to decimal

Integer.parseInt() method returns the integer represented by the string argument in the specified radix.

However, if the String does not contain a parsable int NumberFormatException is thrown.

For example: 1418 = 1×82 + 4×81 + 1×80 = 97

Also read – java program to convert hexadecimal to decimal

Here’s java program to convert octal to decimal.

public class OctalToDecimalDemo
{
   public static void main(String[] args)
   {
      String strOctal = "141";
      // converting octal to decimal
      // number using Integer.parseInt() method
      int decimal = Integer.parseInt(strOctal, 8);
      System.out.println(decimal);
   }
}


Output:

java convert octal to decimal

Octal to decimal converter

Here’s table to convert from octal to decimal converter.

Octal base 8 Decimal base 10
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
10 8
11 9
12 10
13 11
14 12
15 13
16 14
17 15
20 16
30 24
40 32
50 40
60 48
70 56
100 64

program to convert octal to binary

To convert octal to binary first we take input from user to enter octal number using nextInt() method of Scanner class.

This octal number is stored in integer variable ‘octalNumber’. Then use while loop till user entered number is greater than zero.

Also read – continue statement in java

In the next step get the last digit of user entered octal number. Then add each digit by multiplying by power of 8.

Now remove last digit of octal number and increase value of power by 1. Now declare an array ‘binary’ and variable ‘a’ for index value of array.

Then use while loop till decimal number is greater than zero. In the next step divide decimal number by 2.

Also read – how to create directory in java

Store remainder in ‘binary’ array. Repeat until decimal number is greater than zero. Lastly print in reverse order.

Let’s learn java program to convert octal to binary.

import java.util.Scanner;
public class ConvertOctalToBinary
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter an octal number: ");
      int octalNumber = sc.nextInt();
      int deci = 0;
      int num = 0;
      // logic to convert octal to decimal
      while(octalNumber > 0)
      {
         int temp = octalNumber % 10;
         deci += temp * Math.pow(8, num);
         octalNumber = octalNumber / 10;
         num++;
      }
      int[] binary = new int[20];
      int a = 0;
      // logic to convert decimal to binary
      while(deci > 0)
      {
         int b = deci % 2;
         binary[a++] = b;
         deci = deci / 2;
      }
      System.out.println("convert octal to binary: ");
      for(int d = a - 1 ; d >= 0 ; d--)
         System.out.println(binary[d] + " ");
      sc.close();
   }
}


Output:

Please enter an octal number: 644
convert octal to binary: 1 1 0 1 0 0 1 0 0


Meanwhile let’s see octal to decimal conversion using custom method. Here’s the java program.

public class OctalCode
{
   public static int octalToDecimal(int octalNumber)
   {
      int decimal = 0;
      int num = 0;
      while(true)
      {
         if(octalNumber == 0)
         {
            break;
         }
         else
         {
            int temp = octalNumber % 10;
            decimal += temp * Math.pow(8, num);
            octalNumber = octalNumber / 10;
            num++;
         }
      }
      return decimal;
   }
   public static void main(String[] args)
   {
      System.out.println("141 octal is : " + octalToDecimal(141));
      System.out.println("230 octal is : " + octalToDecimal(230));
      System.out.println("100 octal is : " + octalToDecimal(100));
   }
}


Output:

141 octal is : 97
230 octal is : 152
100 octal is : 64


Also read – decimal to binary in java

Click here to learn vice versa of above octal to decimal java program.