Java Program to Convert Octal to Binary
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
class OctalToBinary {
static String converter(String octalValue)
{
int i = 0 ;
String binaryValue = "" ;
while (i < octalValue.length()) {
char c = octalValue.charAt(( int )i);
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++;
}
return binaryValue;
}
public static void main(String args[])
{
System.out.println( "Octal to Binary Conversion\n" );
String octalNumber = "315" ;
System.out.println( "Octal number: " + octalNumber);
String result = converter(octalNumber);
System.out.println( "Binary equivalent value is: "
+ result);
}
}
|
Output
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
public class OctalToBinary {
public static int converter( int octalValue)
{
int i = 0 ;
int decimalValue = 0 ;
int binaryValue = 0 ;
while (octalValue != 0 ) {
decimalValue
+= (octalValue % 10 ) * Math.pow( 8 , i);
++i;
octalValue /= 10 ;
}
i = 1 ;
while (decimalValue != 0 ) {
binaryValue += (decimalValue % 2 ) * i;
decimalValue /= 2 ;
i *= 10 ;
}
return binaryValue;
}
public static void main(String[] args)
{
System.out.println( "Octal to Binary Conversion\n" );
int octalNumber = 315 ;
System.out.println( "Octal number: " + octalNumber);
int result = converter(octalNumber);
System.out.println( "Binary equivalent value is: "
+ result);
}
}
|
Output
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
class OctalToBinary {
public static String converter(String octalValue)
{
int octal = Integer.parseInt(octalValue, 8 );
String binaryValue = Integer.toBinaryString(octal);
return binaryValue;
}
public static void main(String args[])
{
System.out.println( "Octal to Binary Conversion\n" );
String octalNumber = "315" ;
System.out.println( "Octal number: " + octalNumber);
String result = converter(octalNumber);
System.out.println( "Binary equivalent value is: "
+ result);
}
}
|
Output
Octal to Binary Conversion
Octal number: 315
Binary equivalent value is: 11001101
Last Updated :
25 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...