Java Program to Convert from Binary to Octal

Introduction 

This article will illustrate how to convert a binary number to its octal equivalent using two methods:

The Binary System

As the name suggests, the binary number system only consists of two numbers, namely 0 and 1. These numbers often represent the state of a system where 0 represents the low state and 1 represents the high state in digital electronics. It is a number system with a base of 2 and is extensively used in computer science. Bits are sequences of 0s and 1s in any order. Each bit represents either a 0 or a one, and eight such bits make up a byte stored in computer systems for varied purposes. Therefore, a binary number is nothing but a sequence of 0s and 1s determined by the decimal number. Binary numbers can also be thought of as a string of 0s and 1s.

The Octal System

An Octal number system consists of 8 numbers, namely from 0 to 7. The eight numbers in the system come from the word ‘octal’, which means eight. The octal number system is an 8-base number system. Since three bits can represent up to eight unique binary numbers, the octal number system can be formulated by grouping a binary number into three groups. Each group can extract the octal representation of three bits at a time from each group, and the resultant number is the octal equivalent of the given binary number.
Example
Input: 100100
Output: 44
Input 1100001
Output: 141


Note: For binary numbers that cannot be perfectly grouped into threes, padding of zeros is done before the most significant bit of the binary number such that the number of digits is a multiple of 3.

Converting Binary to Octal 

Using built-in toOctal String () method in Java

Converting the binary number to a decimal number can then be converted to the equivalent octal number.

Approach 1

This approach uses the first method, which is to use the toOctalString () method in Java. To apply this, we will first have to convert the binary number to its equivalent integer number and then apply this method. The output from the method will be an octal string. It has to be converted back into an integer.
Syntax: public static String toOctalString (int num)
Parameters: The method will accept a single parameter num of integer type. This number will have to be converted into a string.
Return Value: The method will return a string representation of the octal integer argument as an unsigned integer in base 8.
Algorithm:
The first step is to convert the binary number into a decimal number.
Using this decimal number, we will have to convert this to an octal string using the method provided. The method will return a string of octal numbers.
The string of octal numbers has to be converted into an integer.


Sample Code

//Java Program to convert binary to octal
class Demo
{
//method to convert binary to decimal
int binaryToDecimal (long binary)
{
//variable to store the decimal number
int decimalNumber = 0, i = 0;
//loop to extraxt the digits of the binary number
while (binary > 0)
{
//multiplying each digit of binary with increasing powers of 2 towards left
decimalNumber + = Matho.pow (2, i++) * (binary % 10);
//dividing the binary by 10 to remove the last digit
binary / = 10;
}
//returning the converted decimal to octal
int decimaltoOctal (long binary)
{
//variable to store the decimal number returned by the binaryToDecimal()
int decimalNumber = binaryToDecimal (binary);
//using the toOctalString () to convert integer to string of octal number
String octalString = Integer.toOctalString (decimalNumber);
//converting the String of octal number to an integer
int octalNumber = Integer.parseInt (octalString);
//returning the octal number
return octalNumber;
}
//Driver Code
public static void main (String args [])
{
//instantiating the class
Demo ob = new Demo ();
//calling and printing the decimalToOctal method
System.out.println (“Octal number :” + ob.decimalToOctal (100100));
}
}


OUTPUT

octal number: 44


Approach 2:

This approach will follow the second method mentioned above. Here, we will first convert the binary number to its decimal equivalent. This decimal number will then be converted to its octal equivalent by continuously extracting the remainder and dividing by 8.

Algorithm:
The first step is to convert the binary number to its decimal equivalent.
With this new decimal number, run a loop until the number is not equal to 0.
Obtain the remainder of the number by dividing it by 8 in every iteration.
Multiply the reminder with increasing powers of 10 and divide the original number by 8.

Sample Code

//Java Program to convert binary to octal
class Demo
{
//method to convert binary to decimal
int binaryToDecimal (long binary)
{
//variable to store the decimal number
int decimalNumber = 0, i = 0;
//loop to extraxt the digits of the binary number
while (binary > 0)
{
//multiplying each digit of binary with increasing powers of 2 towards left
decimalNumber + = Matho.pow (2, i++) * (binary % 10);
//dividing the binary by 10 to remove the last digit
binary / = 10;
}
//returning the converted decimal number
return decimalNumber;
}
//function to convert decimal number to octal
int decimalToOctal (long binary)
{
//variable to store the octal number
int octalNumber = 0; i = 0;
//variable to store the output returned by the binaryToDecimal ()
int decimalNumber = binaryToDecimal (binary);
//loop to convert decimal to octal
while (decimalNumber ! = 0)
{
//extracting the remainder on multiplying by 8 and dividing that with increasing powers of 10
octalNumber + = (decimalNumber % 8) * ((int) Math.pow (10, i++));
//removing the last digit by diving by 8
decimalNumber / = 8;
}
//returning the converted octal number
return octalNumber;
}
//Driver Code
public static void main (String args [])
{
//instantiating the class
Demo ob = new Demo ();
//calling and printing the decimalToOctal() function
System.out.println (ob.decimalToOctal (1001001));
}
}

OUTPUT

111

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.