YouTip LogoYouTip

C Examples Octal Binary Convert

## C Program to Convert Binary to Octal and Vice Versa In computer science and digital electronics, binary (base-2) and octal (base-8) are two of the most commonly used numeral systems. Converting between them is a fundamental programming task. This tutorial demonstrates how to convert a binary number to an octal number, and how to convert an octal number back to a binary number using C. --- ## Conversion Methodology While you can convert directly between binary and octal by grouping binary digits into sets of three, the most straightforward programmatic approach for general-purpose variables is to use **Decimal (base-10) as an intermediate bridge**: 1. **Binary to Octal:** `Binary` $\rightarrow$ `Decimal` $\rightarrow$ `Octal` 2. **Octal to Binary:** `Octal` $\rightarrow$ `Decimal` $\rightarrow$ `Binary` --- ## 1. Convert Binary to Octal This program takes a binary number (entered as an integer containing only `0`s and `1`s) and converts it to its octal equivalent. ### Code Example ```c #include #include int convertBinarytoOctal(long long binaryNumber); int main() { long long binaryNumber; printf("Enter a binary number: "); scanf("%lld", &binaryNumber); printf("Binary number %lld in octal is: %d\n", binaryNumber, convertBinarytoOctal(binaryNumber)); return 0; } int convertBinarytoOctal(long long binaryNumber) { int octalNumber = 0, decimalNumber = 0, i = 0; // Step 1: Convert Binary to Decimal while(binaryNumber != 0) { decimalNumber += (binaryNumber % 10) * pow(2, i); ++i; binaryNumber /= 10; } i = 1; // Step 2: Convert Decimal to Octal while (decimalNumber != 0) { octalNumber += (decimalNumber % 8) * i; decimalNumber /= 8; i *= 10; } return octalNumber; } ``` ### Example Output ```text Enter a binary number: 101001 Binary number 101001 in octal is: 51 ``` --- ## 2. Convert Octal to Binary This program takes an octal number (containing digits from `0` to `7`) and converts it to its binary equivalent. ### Code Example ```c #include #include long long convertOctalToBinary(int octalNumber); int main() { int octalNumber; printf("Enter an octal number: "); scanf("%d", &octalNumber); printf("Octal number %d in binary is: %lld\n", octalNumber, convertOctalToBinary(octalNumber)); return 0; } long long convertOctalToBinary(int octalNumber) { int decimalNumber = 0, i = 0; long long binaryNumber = 0; // Step 1: Convert Octal to Decimal while(octalNumber != 0) { decimalNumber += (octalNumber % 10) * pow(8, i); ++i; octalNumber /= 10; } i = 1; // Step 2: Convert Decimal to Binary while (decimalNumber != 0) { binaryNumber += (decimalNumber % 2) * i; decimalNumber /= 2; i *= 10; } return binaryNumber; } ``` ### Example Output ```text Enter an octal number: 51 Octal number 51 in binary is: 101001 ``` --- ## Key Considerations & Limitations * **Data Type Limits:** In these examples, binary numbers are stored as standard integer types (`long long`). Because of this, the maximum length of the binary number you can input is limited by the maximum value of a 64-bit integer (typically up to 19 digits). For extremely large binary or octal numbers, it is recommended to handle the input as a **string** instead of a numeric type. * **Math Library:** The programs use the `pow()` function from the `` library. When compiling this code on Linux/Unix systems using GCC, you may need to link the math library explicitly using the `-lm` flag: ```bash gcc program.c -o program -lm ``` * **Input Validation:** These basic examples assume the user inputs valid binary (only `0`s and `1`s) or valid octal (digits `0` through `7`) numbers. In production environments, you should add validation logic to handle incorrect inputs.
← C Examples Standard DeviationC Examples Calculator Switch C β†’