A binary number uses only 0 and 1 and has a base of 2, while an octal number uses digits from 0 to 7 and has a base of 8. Since 8=23, a binary number can be converted to octal by grouping its digits into sets of three, starting from the right. Each group is then replaced with its corresponding octal digit.
- Binary uses base 2 and contains only 0 and 1.
- Octal uses base 8 and contains digits from 0 to 7.
Illustration
Input:
100100
Output:44Input:
1100001
Output:141
Methods for Binary to Octal Conversion
- Using Integer.toOctalString()
- Using a user-defined method
1. Using Integer.toOctalString()
Java provides the Integer.toOctalString() method to convert a decimal integer into its octal representation. We can first convert the binary number to decimal using Integer.parseInt(binary, 2) and then convert the decimal value to octal.
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.
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));
}
}
Output
octal number:44
Explanation: The binary number "100100" is first converted to decimal, which gives 36. The Integer.toOctalString() method then converts 36 into its octal representation, 44.
2. Using a User-Defined Method
In this approach, we manually convert the binary number to decimal and then convert the decimal number to octal. The binary-to-decimal conversion uses powers of 2, while the decimal-to-octal conversion repeatedly divides the number by 8 and stores the remainders.
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.
// 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));
}
}
Output
111
Explanation: The binaryToDecimal() method processes each binary digit from left to right and calculates its decimal value. The decimalToOctal() method repeatedly divides the decimal number by 8 and collects the remainders. Finally, the remainders are reversed to obtain the octal number.