Convert Octal to Binary in Java



Both binary and octal are types of number systems with the base value 2 and 8, respectively. This article aims to explain how to convert octal to binary in Java, which is one of the frequently asked interview questions.

A number system is a way of representing numbers. Each type of number system has a different base value which depends on the number of digits contained by them.

What is Binary Number System?

There are four types of number systems available. Binary numbers are one of them. It is represented with two digits i.e. one (1) and zero (0). The binary numbers are expressed as base-2 in the numeral system.

What is Octal Number System?

Octal number is also one of the number systems available. The octal number is represented with 8 digits which is from 0 to 7 (0, 1, 2, 3... 7). The Octal numbers are expressed as base-8 in the numeral system.

Octal to Binary Conversion in Java

To convert the octal number system to a binary number, we can use the following approaches:

  • Naive approach
  • Using Mathematical calculation
  • Using parseInt() and toBinaryString() methods

Example Scenario

Input: octalNumber = 235;
Output: binaryNumber = 010011101

Calculation process:

The binary of 2 = 010
The binary of 3 = 011
The binary of 5 = 101

Naive approach to convert Octal into Binary

In this approach, we maintain a data set containing the binary representations of octal digits (0 to 7). After getting any octal number to convert, we divide it into individual digits, compare each digit with the data set, and select the corresponding binary values. Finally, we concatenate these binary values to obtain the binary representation of the original octal number.

Example

The following example is the implementation of approach discussed above.

public class Main {
   public static void main(String args[]){
      String inputNumber = "432";
      System.out.println("Given Octal number: " + inputNumber);
      String res = octalToBinary(inputNumber);
      System.out.println("The Binary number of given octal number is: "+ res);
   }
   static String octalToBinary(String octalNum){
      int i = 0;
      String binary = "";
      while (i < octalNum.length()) {
         char c = octalNum.charAt((int)i);
         switch (c) {
            case '0':
            binary += "000";
            break;
            
            case '1':
            binary += "001";
            break;
            
            case '2':
            binary += "010";
            break;
 
            case '3':
            binary += "011";
            break;
 
            case '4':
            binary += "100";
            break;
            
            case '5':
            binary += "101";
            break;
 
            case '6':
            binary += "110";
            break;
 
            case '7':
            binary += "111";
            break;
            default:
            System.out.println( "\nYou provide an invalid Octal Digit- " + octalNum.charAt((int)i));
            break;
         }
         i++;
      }
   return binary;
   }
}

On running this code, it will show the below output ?

Given Octal number: 432
The Binary number of given octal number is: 100011010

Using Mathematical calculation

Another approach to solving this problem is to first convert the octal number to a decimal and then from decimal to binary. This method involves mathematical calculations and is more efficient than the previously discussed approach.

Example

In the below example, we are converting octal to binary using mathematical calculation.

public class Main {
   public static String convertOctToBinary(int octalNum) {
      int decimalNum = 0, i = 0;
      while (octalNum != 0) {
         decimalNum += (octalNum % 10) * Math.pow(8, i++);
         octalNum /= 10;
      }
       return Integer.toBinaryString(decimalNum);
    }    
   public static void main(String args[]) {
      System.out.print("After converting Octal to Binary: ");
      System.out.println(convertOctToBinary(432));
   }
}

When you execute this code, it will generate following output ?

After converting Octal to Binary: 100011010

Using Integer.parseInt() methods

We will pass the octal number and its base value as parameters to the Integer.parseInt() method. This method will parse the octal number into a primitive integer value.

Example

In the following example, we are converting octal to binary using Integer.parseInt() method.

public class Main {
   public static String convertOctToBinary(String octalNum) {
      int decimalNum = Integer.parseInt(octalNum, 8);
      return Integer.toBinaryString(decimalNum);
    }    
   public static void main(String args[]) {
      System.out.print("After converting Octal to Binary: ");
      System.out.println(convertOctToBinary("2345"));
   }
}

When you execute this code, it will generate following output ?

After converting Octal to Binary: 10011100101
Updated on: 2024-08-01T10:56:06+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements