Placement Prep

Octal to Binary Conversion in C, C++, Java and Python

Step-by-step octal to binary conversion with two methods, worked examples, and ready-to-run code in C, C++, Java and Python.

By FACE Prep Team 6 min read
number-systems octal binary c cpp java python

Octal to binary conversion follows two distinct paths: the two-step method uses decimal as an intermediate, while digit-by-digit mapping converts each octal digit to three binary bits directly.

Both approaches produce the same result. The choice between them depends on context: the two-step method generalises to other base-conversion problems and tends to appear in placement aptitude tests, while the digit-mapping approach is faster to execute by hand and reinforces the structural relationship between octal and binary bases.

Where This Appears in Placement Tests

Number-system conversion questions appear in the aptitude sections of placement tests at companies like TCS, Wipro, and Cognizant. The typical question asks either for a numerical answer (“what is octal 57 in binary?”) or for a code implementation (“write a function to convert octal to binary”). Both forms show up in written tests and in technical interview rounds focused on fundamentals.

The Unix filesystem connection makes this more than a textbook exercise. File permissions in Unix-based operating systems are written in octal notation: chmod 755 sets permissions that a developer reads as three 3-bit groups in binary (rwxr-xr-x). Understanding the conversion concretely makes that notation readable rather than a memorised incantation. This topic connects naturally to data structures and number-system questions that companies include in the same aptitude round.

Freshers from Tier-2 and Tier-3 colleges often encounter these questions in the first filter test before ever reaching a coding interview. Getting the conversion right by hand, without a calculator, is the differentiator.

The Digit-Mapping Shortcut

Octal is base 8, and 8 equals 2^3. That relationship means each octal digit maps to exactly three binary digits, with no decimal intermediate needed. The full lookup:

Octal digitBinary (3 bits)
0000
1001
2010
3011
4100
5101
6110
7111

To convert octal 173 to binary using this table:

  • 1 maps to 001
  • 7 maps to 111
  • 3 maps to 011
  • Concatenated: 001111011
  • Strip leading zeros: 1111011

Verification: octal 173 is (1 x 64) + (7 x 8) + (3 x 1) = 64 + 56 + 3 = 123 in decimal. Binary 1111011 is 64 + 32 + 16 + 8 + 0 + 2 + 1 = 123. Both routes match.

This method scales to any number of digits without arithmetic. It’s the reason engineers prefer octal for compact binary representation: a 3-digit octal number represents 9 bits in one notation step.

The Two-Step Method: Octal to Decimal, then Binary

The two-step method is what placement test solutions typically expect, because the same logic applies to any base pair, not just octal and binary.

Step 1: Octal to Decimal

Multiply each digit by 8 raised to its positional index, counting from the right starting at zero, then sum the results.

For octal 17:

  • Position 0 (rightmost): 7 x 8^0 = 7 x 1 = 7
  • Position 1: 1 x 8^1 = 1 x 8 = 8
  • Decimal sum: 7 + 8 = 15

Step 2: Decimal to Binary

Divide the decimal result by 2 repeatedly and record the remainder at each step. Read the remainders from bottom to top.

For decimal 15:

  • 15 / 2 = 7, remainder 1
  • 7 / 2 = 3, remainder 1
  • 3 / 2 = 1, remainder 1
  • 1 / 2 = 0, remainder 1
  • Read remainders upward: 1111

Octal 17 = decimal 15 = binary 1111. The digit-mapping shortcut gives the same answer: 1 maps to 001, 7 maps to 111, concatenated 001111, stripped to 1111.

The two-step method works for any octal input, but becomes tedious for long numbers by hand. For inputs with 4 or more digits, the digit-mapping approach is faster and less error-prone.

Code in C, C++, Java and Python

Each implementation below follows the two-step approach. The functions are split so you can substitute either step independently.

C

#include <stdio.h>

int octalToDecimal(int octal) {
    int decimal = 0, base = 1;
    while (octal != 0) {
        decimal += (octal % 10) * base;
        base *= 8;
        octal /= 10;
    }
    return decimal;
}

long long decimalToBinary(int decimal) {
    long long binary = 0, place = 1;
    while (decimal > 0) {
        binary += (decimal % 2) * place;
        decimal /= 2;
        place *= 10;
    }
    return binary;
}

int main() {
    int octal = 17;
    int decimal = octalToDecimal(octal);
    long long binary = decimalToBinary(decimal);
    printf("Octal %d = Binary %lld\n", octal, binary);
    return 0;
}

C++

#include <iostream>
using namespace std;

int octalToDecimal(int octal) {
    int decimal = 0, base = 1;
    while (octal != 0) {
        decimal += (octal % 10) * base;
        base *= 8;
        octal /= 10;
    }
    return decimal;
}

long long decimalToBinary(int decimal) {
    long long binary = 0, place = 1;
    while (decimal > 0) {
        binary += (decimal % 2) * place;
        decimal /= 2;
        place *= 10;
    }
    return binary;
}

int main() {
    int octal = 17;
    int decimal = octalToDecimal(octal);
    long long binary = decimalToBinary(decimal);
    cout << "Octal " << octal << " = Binary " << binary << endl;
    return 0;
}

Java

public class OctalToBinary {
    static int octalToDecimal(int octal) {
        int decimal = 0, base = 1;
        while (octal != 0) {
            decimal += (octal % 10) * base;
            base *= 8;
            octal /= 10;
        }
        return decimal;
    }

    static long decimalToBinary(int decimal) {
        long binary = 0, place = 1;
        while (decimal > 0) {
            binary += (decimal % 2) * place;
            decimal /= 2;
            place *= 10;
        }
        return binary;
    }

    public static void main(String[] args) {
        int octal = 17;
        int decimal = octalToDecimal(octal);
        long binary = decimalToBinary(decimal);
        System.out.println("Octal " + octal + " = Binary " + binary);
    }
}

Python

def octal_to_decimal(octal):
    decimal, base = 0, 1
    while octal != 0:
        decimal += (octal % 10) * base
        base *= 8
        octal //= 10
    return decimal

def decimal_to_binary(decimal):
    binary, place = 0, 1
    while decimal > 0:
        binary += (decimal % 2) * place
        decimal //= 2
        place *= 10
    return binary

octal = 17
decimal = octal_to_decimal(octal)
binary = decimal_to_binary(decimal)
print(f"Octal {octal} = Binary {binary}")

These implementations store the binary result as a numeric type, which works cleanly for small inputs. For larger octal numbers, build the binary result as a string instead of accumulating place-value digits into an integer. The same iterative structure applies to iterative array problems of the kind commonly tested alongside number-system questions.

Built-in One-Liners

Both Python and Java include standard library functions that handle the conversion in a single expression. These are the practical choice when writing placement-test code under time pressure.

Python uses the built-in bin() function combined with int() accepting a base argument:

octal_str = "17"
binary_str = bin(int(octal_str, 8))[2:]
print(binary_str)  # Output: 1111

int("17", 8) parses the string as base-8, giving 15. bin(15) returns "0b1111". The [2:] slice removes the 0b prefix.

Java uses Integer.toBinaryString() combined with Integer.parseInt():

String binaryStr = Integer.toBinaryString(Integer.parseInt("17", 8));
System.out.println(binaryStr); // Output: 1111

Both one-liners follow the same pattern: parse with a base, convert with a library function. You’ll find an identical pattern in a palindrome check or any other problem where Java and Python offer a string-manipulation shortcut over an explicit loop.

The manual implementations from the previous section remain useful for interviews where interviewers ask you to demonstrate understanding without built-ins.

Time Complexity and Edge Cases

Both conversion methods run in O(d) time, where d is the number of octal digits:

  • The two-step method processes each octal digit once during the positional-weight pass, and then processes each binary bit once during the division-by-2 pass. The number of binary bits is proportional to d (since each octal digit expands to 3 bits), so the total remains O(d).
  • The digit-mapping method processes each digit exactly once via a table lookup, with no arithmetic at all.

Space complexity is O(b) where b is the number of binary digits in the output, since the result must be stored. For the integer-accumulator implementations above, b is bounded by the word size of the data type in use.

Three edge cases to handle in a production implementation:

  • Octal 0 should return binary 0 (the while loops above exit immediately for decimal = 0, returning 0).
  • Leading zeros in the octal input are valid and produce leading zeros in intermediate concatenation; strip them at the output stage.
  • Negative octal numbers require sign handling separate from the magnitude conversion; the implementations above are for non-negative integers only.

From Bit Arithmetic to Deployed Models

The digit-by-digit shortcut above works because 8 equals 2^3, making octal and binary evenly related at every digit boundary. That same powers-of-2 relationship governs how machine learning frameworks store tensor values: INT8, FP16, and BF16 are all binary layouts that practitioners encounter when deploying quantized models. Understanding positional binary arithmetic is the prerequisite that makes those format descriptions readable rather than opaque. TinkerLLM at ₹299 is where that shift from conversion exercises to hands-on model deployment starts.

Primary sources

Frequently asked questions

Is there a direct way to convert octal to binary without going through decimal?

Yes. Each octal digit maps to a 3-bit group: 0 to 000, 1 to 001, 2 to 010, 3 to 011, 4 to 100, 5 to 101, 6 to 110, 7 to 111. Concatenate all groups and strip leading zeros.

What is the binary equivalent of octal 17?

Octal 17 is binary 1111. Via digit mapping: 1 maps to 001 and 7 maps to 111, concatenated as 001111, stripped to 1111. Via decimal: octal 17 is 15 in decimal, and 15 in binary is 1111.

How do I convert octal to binary in Java using built-in methods?

Use Integer.toBinaryString(Integer.parseInt(octalString, 8)). The parseInt call with radix 8 parses the octal string; toBinaryString returns the binary string representation.

Why does each octal digit map to exactly 3 binary digits?

Because octal is base 8 and 8 equals 2 raised to the power 3. Each octal digit from 0 through 7 requires exactly 3 bits to represent, ranging from 000 to 111.

What is the time complexity of octal to binary conversion?

O(d) where d is the number of octal digits. Each digit is processed in constant steps whether you use digit mapping or the two-step decimal method.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF