A Binary Number System is composed of two symbols : 0’s (zeroes) and 1’s (ones), which represent low or Off and high or On state respectively in digital electronics. It is primarily a number system with the base-2 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 0’s and 1’s.
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.
For example Binary to Octal Conversion
Input : 100100
Output: 44
Input : 1100001
Output : 141
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 in Java
- Converting the binary number to a decimal number which is further converted to the corresponding Octal number.
1. Using toOctalString() Method for Binary to Octal Conversion
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.
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.
Below is the implementation of the above method:
Java
class GFG {
int binaryToDecimal( long binary)
{
int decimalNumber = 0 , i = 0 ;
while (binary > 0 ) {
decimalNumber
+= Math.pow( 2 , i++) * (binary % 10 );
binary /= 10 ;
}
return decimalNumber;
}
int decimalToOctal( long binary)
{
int decimalNumber = binaryToDecimal(binary);
String octalString
= Integer.toOctalString(decimalNumber);
int octalNumber = Integer.parseInt(octalString);
return octalNumber;
}
public static void main(String[] args)
{
GFG ob = new GFG();
System.out.println( "octal number:"
+ ob.decimalToOctal( 100100 ));
}
}
|
2. User Defined Function to Convert Binary to Octal
Using this approach, we first convert the binary number to a decimal number. We 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
class GFG {
int binaryToDecimal( long binary)
{
int decimalNumber = 0 , i = 0 ;
while (binary > 0 ) {
decimalNumber
+= Math.pow( 2 , i++) * (binary % 10 );
binary /= 10 ;
}
return decimalNumber;
}
int decimalToOctal( long binary)
{
int octalNumber = 0 , i = 0 ;
int decimalNumber = binaryToDecimal(binary);
while (decimalNumber != 0 ) {
octalNumber += (decimalNumber % 8 )
* (( int )Math.pow( 10 , i++));
decimalNumber /= 8 ;
}
return octalNumber;
}
public static void main(String[] args)
{
GFG ob = new GFG();
System.out.println(ob.decimalToOctal( 1001001 ));
}
}
|
The complexity of the above method:
Time complexity: O(nlogn) as using pow function inside while loop
Auxiliary space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
31 Jul, 2023
Like Article
Save Article