Given an Octal number as input, the task is to convert that number into its Binary equivalent number.
Example:
Input: Octal Number = 513 Output: Binary equivalent value is: 101001011 Explanation : Binary equivalent value of 5: 101 Binary equivalent value of 1: 001 Binary equivalent value of 3: 011
Octal Number System:
The Octal number system is a positional numeral system with a radix, or base, of 8 and uses eight distinct digits 0 to 7.
Binary Number System:
A Binary number is a number expressed in the base-2 binary numeral system, which uses only two symbols: which are 0 and 1.
Octal to Binary Conversion Table:
Octal | Binary |
---|---|
0 | 000 |
1 | 001 |
2 | 010 |
3 | 011 |
4 | 100 |
5 | 101 |
6 | 110 |
7 | 111 |
Method 1: Naive Approach
In this approach, the input of Octal number will be taken as a string. Further, the complete string will be iterated character by character and for each character(ie., octal digit), its equivalent binary value according to the above octal to binary conversion table will be evaluated. The result of each iteration will be added in the resultant string and the combination of all values will give the required binary number. Below is the implementation of this approach.
Java
// Java program to convert // Octal number to Binary class OctalToBinary { // function to convert octal number // to its binary equivalent value static String converter(String octalValue) { // integer variable to iterate // the input octal string int i = 0 ; // string to store the result String binaryValue = "" ; // iterating the complete length // of octal string and assigning // the equivalent binary value // for each octal digit while (i < octalValue.length()) { // storing character according // to the number of iteration char c = octalValue.charAt(( int )i); // switch case to check all // possible 8 conditions switch (c) { case '0' : binaryValue += "000" ; break ; case '1' : binaryValue += "001" ; break ; case '2' : binaryValue += "010" ; break ; case '3' : binaryValue += "011" ; break ; case '4' : binaryValue += "100" ; break ; case '5' : binaryValue += "101" ; break ; case '6' : binaryValue += "110" ; break ; case '7' : binaryValue += "111" ; break ; default : System.out.println( "\nInvalid Octal Digit " + octalValue.charAt(( int )i)); break ; } i++; } // returning the final result return binaryValue; } // Driver code public static void main(String args[]) { System.out.println( "Octal to Binary Conversion\n" ); // octal number which is to be converted String octalNumber = "315" ; System.out.println( "Octal number: " + octalNumber); // calling the converter method and // storing the result in a string variable String result = converter(octalNumber); System.out.println( "Binary equivalent value is: " + result); } } |
Octal to Binary Conversion Octal number: 315 Binary equivalent value is: 011001101
Method 2: Mathematical Approach
This method involves complete mathematical calculations to obtain the desired result. The input of the Octal number will be taken as an integer. Further, it is first converted into its decimal equivalent value and then from a decimal number to its binary equivalent using mathematical operations. Below is the implementation of this approach.
Java
// Java program to convert // Octal number to Binary public class OctalToBinary { // function to convert octal number // to its binary equivalent value public static int converter( int octalValue) { // declaring all variable // to store the intermediate results int i = 0 ; int decimalValue = 0 ; int binaryValue = 0 ; // converting octal number // into its decimal equivalent while (octalValue != 0 ) { decimalValue += (octalValue % 10 ) * Math.pow( 8 , i); ++i; octalValue /= 10 ; } i = 1 ; // converting generated decimal number // to its binary equivalent while (decimalValue != 0 ) { binaryValue += (decimalValue % 2 ) * i; decimalValue /= 2 ; i *= 10 ; } // returning the final result return binaryValue; } // Driver code public static void main(String[] args) { System.out.println( "Octal to Binary Conversion\n" ); // octal number which is to be converted int octalNumber = 315 ; System.out.println( "Octal number: " + octalNumber); // calling the converter method and // storing the result in a string variable int result = converter(octalNumber); // printing the binary equivalent value System.out.println( "Binary equivalent value is: " + result); } } |
Octal to Binary Conversion Octal number: 315 Binary equivalent value is: 11001101
Method 3: Using inbuilt java methods
Integer.parseInt() is an in-built function in Java to parse a string into a number system specified by the radix value(2nd argument of the method). In this approach, the input of the Octal number will be taken as a string. The octal string will be parsed into an integer value of octal number system. Further, the octal number will be converted into its binary equivalent using another inbuilt method Integer.toBinaryString(). The resultant value will also be of string datatype. Below is the implementation.
Java
// Java program to convert // Octal number to Binary class OctalToBinary { // function to convert octal number // to its binary equivalent value public static String converter(String octalValue) { // parsing the string value // by following octal number system int octal = Integer.parseInt(octalValue, 8 ); // converting octal number to binary // and storing as a string String binaryValue = Integer.toBinaryString(octal); // returning the resultant string return binaryValue; } // Driver code public static void main(String args[]) { System.out.println( "Octal to Binary Conversion\n" ); // octal number which is to be converted String octalNumber = "315" ; System.out.println( "Octal number: " + octalNumber); // calling the converter method and // storing the result in a string variable String result = converter(octalNumber); // printing the binary equivalent value System.out.println( "Binary equivalent value is: " + result); } } |
Octal to Binary Conversion Octal number: 315 Binary equivalent value is: 11001101