Hex to octal conversion
This hex to octal converter changes base-16 numbers into base 8. Both bases are powers of two, yet their digits do not line up (hex digits cover 4 bits, octal digits cover 3), so the clean way to convert is to go through binary.
The binary bridge
- Expand each hex digit into four bits.
- Regroup the bits into threes, starting from the right.
- Replace each 3-bit group with an octal digit.
1ED
→ 0001 1110 1101 (hex digits as 4 bits)
→ 000 111 101 101 (regrouped in threes)
→ 0 7 5 5 = 755
The same result via decimal takes more work: 1ED is 493, and 493 divided repeatedly by 8 also gives 755. The binary route avoids division entirely.
Example
Input Output
A 12
FF 377
1F4 764
1ED 755
7FFFFFFF 17777777777
A.8 12.4
Fractions regroup from the point outward. A.8 is 1010.1 in binary, which becomes 001 010 . 100, or 12.4 in octal.
A practical case: file modes
Tools that dump raw structures, such as hex editors or debuggers, may show a file's st_mode in hexadecimal. The value 81A4 converts to 100644: a regular file with permissions 644 (rw-r--r--). 81ED gives 100755, an executable file with rwxr-xr-x. Git uses these same numbers, 100644 and 100755, for file modes in its trees.
Input rules
0xand#prefixes are removed, and letters can be upper or lower case.- Spaces, commas and underscores are ignored, and the field holds one number at a time.
- Integers of any length convert exactly; fractions stop after 20 octal places.
Common mistakes
- Dropping zeros inside the number. Every hex digit except the first must expand to a full four bits. F2 is 1111 0010; writing the 2 as just 10 gives 111110 and the wrong answer 76 instead of 362.
- Regrouping from the left. The eight bits of FF grouped from the left (111 111 11) do not make sense; grouped from the right (11 111 111) they give 377.
- Reading the result as decimal. Octal 377 is 255, not three hundred seventy-seven.
To go the other way, use octal to hex. The intermediate steps are easy to check yourself with hex to binary and binary to octal.
Writing the result in code
Octal literals are spelled differently from language to language, and a wrong prefix silently changes the value:
| Where | 755 octal written as |
|---|---|
| C, C++, Go (classic form) | 0755 |
| Python 3, JavaScript, Rust, Go | 0o755 |
| chmod, umask | 755 (always read as octal) |
In JavaScript, a leading zero such as 0755 is a legacy form that is a syntax error in strict mode, and Python 3 rejects it entirely. In a shell you can check a single value without any tool: printf '%o\n' 0x1ED prints 755. For values longer than 64 bits, or with a fractional part, the converter above is the easier route.