Binary numbers use base-2, while octal numbers use base-8. Converting binary to octal is useful in programming, particularly for compact representations of binary data or low-level system tasks. In this article, we will write C programs to convert a binary number into its octal equivalent.
Understanding The Problem
Each octal digit represents three binary digits. To convert binary to octal, the binary number is grouped into sets of three bits starting from the right. Each group is then converted into a decimal digit to form the octal number. For example, binary 101101
is grouped as 101
and 101
, which converts to octal 55
.
Program 1: Using Arithmetic Method
We first convert the binary number into decimal and then convert that decimal into octal.
#include <stdio.h>
#include <math.h>
int main() {
long long binary;
int decimal = 0, octal = 0, i = 0, rem;
printf("Enter a binary number: ");
scanf("%lld", &binary);
while (binary > 0) {
rem = binary % 10;
decimal += rem * pow(2, i);
binary /= 10;
i++;
}
i = 1;
while (decimal > 0) {
rem = decimal % 8;
octal += rem * i;
decimal /= 8;
i *= 10;
}
printf("Octal equivalent: %d\n", octal);
return 0;
}
This method first converts the binary number to decimal, then constructs the octal number by repeatedly dividing by 8. It works for small to medium-sized binary inputs.
Program 2: Using Strings
This method processes the binary number as a string, pads it to a multiple of three digits, and converts each group into an octal digit.
#include <stdio.h>
#include <string.h>
#include <math.h>
int main() {
char binary[65];
int len, i, octalDigit;
printf("Enter a binary number: ");
scanf("%s", binary);
len = strlen(binary);
int pad = (3 - len % 3) % 3;
char paddedBinary[68] = {0};
for (i = 0; i < pad; i++) {
paddedBinary[i] = '0';
}
strcpy(paddedBinary + pad, binary);
printf("Octal equivalent: ");
for (i = 0; i < strlen(paddedBinary); i += 3) {
octalDigit = (paddedBinary[i] - '0') * 4 +
(paddedBinary[i+1] - '0') * 2 +
(paddedBinary[i+2] - '0') * 1;
printf("%d", octalDigit);
}
printf("\n");
return 0;
}
This string-based method is safe for large binary inputs and avoids integer overflow by treating the number as text.
Program 3: Using sprintf()
with Intermediate Decimal
For modern C, we can use sprintf()
to simplify the decimal-to-octal conversion after converting the binary input to decimal.
#include <stdio.h>
#include <math.h>
int main() {
long long binary;
int decimal = 0, i = 0, rem;
char octal[32];
printf("Enter a binary number: ");
scanf("%lld", &binary);
// Convert binary to decimal
while (binary > 0) {
rem = binary % 10;
decimal += rem * pow(2, i);
binary /= 10;
i++;
}
// Convert decimal to octal string using sprintf
sprintf(octal, "%o", decimal); // %o for octal formatting
printf("Octal equivalent: %s\n", octal);
return 0;
}
This method combines the safety of string output with simple formatting. sprintf()
automatically handles decimal-to-octal conversion, producing a clean result.
Program 4: Using strtol()
strtol()
can convert a binary string directly to decimal with base 2, and then %o
prints it as octal.
#include <stdio.h>
#include <stdlib.h>
int main() {
char binaryStr[65];
unsigned long decimal;
printf("Enter a binary number: ");
scanf("%s", binaryStr);
// Convert binary string to decimal using strtol with base 2
decimal = strtol(binaryStr, NULL, 2);
// Print octal equivalent
printf("Octal equivalent: %lo\n", decimal);
return 0;
}
This approach is safe for large binary inputs and avoids manual arithmetic or string grouping. Itβs concise and reliable.
FAQs
Q1: Can this program handle very large binary numbers?
Yes, the string-based and sprintf()
methods are safer for large binary numbers, while the arithmetic method may overflow.
Q2: Why group binary digits in threes?
Each octal digit corresponds exactly to three binary digits, making conversion straightforward.
Q3: Which method is easiest?
Using sprintf()
is concise and avoids manual calculations, while the string method is robust for long binary sequences.
Conclusion
Binary-to-octal conversion is practical for programmers dealing with low-level data. Arithmetic, string-based, and sprintf()
methods all provide accurate results. Understanding these approaches allows safe and efficient conversions across different number systems.
References & Additional Resources
- C Programming Tutorial β Beginner-friendly guide to loops, arrays, and conversions.
- GeeksforGeeks – Binary to Octal β Explains multiple methods of conversion.
- TutorialsPoint – Number Systems β Complete guide to binary, octal, decimal, and hexadecimal.