AlgoInsight

Q&A

How to convert octal to hexadecimal

By AlgoInsight Team2026-06-07math

Overview

Number bases represent the same value in different radixes: octal uses digits 0–7, hexadecimal uses 0–9 then A–F (where A=10 … F=15). Because 8 = 2³ and 16 = 2⁴, binary is the natural bridge between them: octal maps cleanly to groups of 3 bits, hex to groups of 4 bits.

graph LR
    A[Octal base-8] -->|each digit → 3 bits| B[Binary base-2]
    B -->|group into 4-bit nibbles| C[Hexadecimal base-16]
    A -.alternative.-> D[Decimal base-10] -.-> C
    style B fill:#1e1b4b,color:#fff

Method 1: Octal → Binary → Hex (most reliable by hand)

Step 1 - Octal to binary. Replace each octal digit with its 3-bit binary equivalent.

Octal Binary Octal Binary
0 000 4 100
1 001 5 101
2 010 6 110
3 011 7 111

Step 2 - Group binary into 4-bit nibbles from the right (pad the left with zeros) and convert each nibble to a hex digit.

Worked example: octal 725

Octal:    7    2    5
Binary: 111  010  101   →  111010101
Group into 4 bits (right→left), pad left:  0001 1101 0101
Hex:        1    D    5

So 725₈ = 1D5₁₆.

Method 2: Octal → Decimal → Hex

Step 1 - Octal to decimal: multiply each digit by 8 raised to its position.

725₈ = 7×8² + 2×8¹ + 5×8⁰ = 448 + 16 + 5 = 469

Step 2 - Decimal to hex: repeatedly divide by 16, reading remainders bottom-up.

469 ÷ 16 = 29 r 5
 29 ÷ 16 =  1 r 13 (D)
  1 ÷ 16 =  0 r 1
Reading remainders upward: 1 D 5  → 1D5₁₆

Both methods agree: 725₈ = 1D5₁₆.

In Code

Python

octal_str = "725"
value = int(octal_str, 8)        # parse base-8 -> 469
hex_str = format(value, "X")     # to uppercase hex -> "1D5"
print(hex_str)                   # 1D5
# one-liner:
print(format(int("725", 8), "X"))

Java

int value = Integer.parseInt("725", 8);   // octal -> int (469)
String hex = Integer.toHexString(value).toUpperCase();  // "1D5"

JavaScript

const value = parseInt("725", 8);   // 469
const hex = value.toString(16).toUpperCase();   // "1D5"

C

#include <stdio.h>
int main() {
    int value;
    sscanf("725", "%o", &value);   // read as octal
    printf("%X\n", value);          // print as hex -> 1D5
}

Quick Reference

Why This Matters in Interviews

Base-conversion questions test whether you understand positional notation and the 2³/2⁴ relationship that makes binary the bridge. Be able to do 725₈ → 1D5₁₆ by hand both ways, and know the one-line built-in conversions in your language. Explaining why you can't map octal digits directly to hex digits is the key insight.

Common Pitfalls

References

More answers