Java Program to Convert Binary to Octal (original) (raw)
Last Updated : 23 Apr, 2025
A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.
**Example of Binary to Octal Conversion:
**Input : 100100
**Output: 44**Input : 1100001
**Output : 141
A **Binary Number System is composed of two symbols: 0s (zeroes) and 1s (ones), which represent low or **off and high or **on states, respectively, in digital electronics. It is primarily a number system with a base and is extensively used in computer science as well. All the data is stored in binary symbols in computers, which are also called bits. The Binary System derives its name from the fact that it is composed of just two symbols. A Binary Number can also be thought of as a string of just 0s and 1s.
An **Octal Number System comprises eight digits ranging from **0 to 7. It derives its name from the fact that it consists of eight digits (hence Oct), which means eight. It is an 8-base number system and can be formulated by grouping the bits in a binary number in groups of three and calculating the corresponding value of each group as a single digit in the resultant Octal Number.
Methods for Binary to Octal Conversion
In this article, we are going to explore 2 methods to convert a Binary Number to an Octal Number. These methods are as follows :
- Using the built-in **toOctalString()method
- Using User-defined method
**1. Using toOctalString()
Using this approach, we first convert a binary number to an Integer and then using the toOctalString() built-in method of Java, convert it into a string of octal numbers. This string is then converted to an Integer again.
**Syntax of toOctalString():
public static String toOctalString(_int num)
- **Parameters: The method accepts a single parameter num of integer type which is required to be converted to a string.
- **Return Value: The function returns a string representation of the integer argument as an unsigned integer in base 8 (Octal number).
**Algorithm:
- Convert the binary number into a decimal number.
- Convert this decimal number to a string of octal numbers using the toOctalString() method.
- Convert this string again to an Integer.
**Example: Java program to convert a number binary to octal using **toOctalString() method.
Java `
// Java program to convert binary to octal // using toOctalString() method class Geeks { // method to convert binary to decimal int binaryToDecimal(long binary) { // variable to store the decimal number int decimalNumber = 0, i = 0;
// loop to extract the digits of the
// binary number
while (binary > 0) {
// multiplying each digit of binary
// with increasing powers of 2 towards
// left
decimalNumber
+= Math.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 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
Geeks ob = new Geeks();
// calling and printing the decimalToOCtal
// method
System.out.println("octal number:"
+ ob.decimalToOctal(100100));
}
}
`
**2. User-defined Method
Using this approach, we first convert the binary number to a decimal number. Then convert this decimal number to an octal number by continuously extracting the remainder and dividing by 8.
**Algorithm:
- Convert the binary number to a decimal number.
- Now, for this converted decimal number, run a while loop till this number becomes 0.
- In every iteration of the loop, get the remainder by dividing the number by 8.
- Multiply this remainder with increasing powers of 10.
- Finally, divide the original number by 8.
**Example: Java program to convert a number binary to octal using **user-defined function.
Java `
// Java program to convert binary to octal // using user-defined function
// Driver Class class Geeks { // function to convert binary number // to decimal number int binaryToDecimal(long binary) { // variable to store the converted // decimal number int decimalNumber = 0, i = 0;
// loop to convert binary to decimal
while (binary > 0) {
// extracting every digit of the
// binary and multiplying with
// increasing powers of 2
decimalNumber
+= Math.pow(2, i++) * (binary % 10);
// dividing the number 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
// dividing by 8
decimalNumber /= 8;
}
// returning the converted octal number
return octalNumber;
}
// Driver Code
public static void main(String[] args)
{
// instantiating the class
Geeks ob = new Geeks();
// calling and printing the
// decimalToOctal() function
System.out.println(ob.decimalToOctal(1001001));
}
}
`