Java Program to Convert Octal to Binary
Last Updated :
25 Aug, 2021
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);
}
}
OutputOctal 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);
}
}
OutputOctal 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);
}
}
OutputOctal to Binary Conversion
Octal number: 315
Binary equivalent value is: 11001101
Similar Reads
Java Tutorial
Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read