How do you convert hex to octal?
The cleanest route goes through binary. Write each hex digit as 4 bits, then regroup the whole bit string into 3-bit chunks starting from the right, and convert each chunk to a digit from 0 to 7. Hex 1ED is 0001 1110 1101 in binary, regrouped as 000 111 101 101, which reads as octal 755.
You can't map hex digits straight to octal digits, because 4-bit and 3-bit groups only line up every 12 bits. That's what makes this conversion more error-prone by hand than hex to binary or binary to octal.
Using the tool
- Enter hex digits, for example
1EDor1ed. - Click Convert.
- Copy the octal result,
755.
Don't include 0x, and don't put spaces inside the value. Leading and trailing whitespace is fine.
Worked example: 1ED, regrouping the bits
Step 1: expand each hex digit to four bits.
| Hex digit | 1 | E | D |
|---|---|---|---|
| Bits | 0001 | 1110 | 1101 |
Step 2: join and regroup in threes from the right. The 12 bits 000111101101 become 000 111 101 101.
Step 3: read each triplet. 000 is 0, 111 is 7, 101 is 5, 101 is 5. Drop the leading zero and the answer is 755.
If the bit count isn't a multiple of three, pad the leftmost group with zeros. Always regroup from the right-hand end, the ones place, or every digit shifts.
Octal escapes for bytes
The most practical reason to convert hex to octal is writing byte values as octal escape sequences. C, shell printf and Python string literals all accept a backslash followed by up to three octal digits (Python's escape sequence table lists \ooo). Hex dumps show bytes in hex, so you convert:
| Byte (hex) | Octal | Escape | What it is |
|---|---|---|---|
0a |
12 | \012 |
line feed |
1b |
33 | \033 |
ESC, the start of ANSI terminal color codes like \033[0m |
20 |
40 | \040 |
space |
41 |
101 | \101 |
the letter A |
7f |
177 | \177 |
DEL |
ff |
377 | \377 |
highest byte value |
Pad short results to three digits when you write the escape (\012, not \12), so a following digit isn't swallowed into the escape.
File modes from a hex dump
Unix stores a file's type and permissions in one number, st_mode, which is conventionally read in octal. In a binary dump of a filesystem or archive header you'll meet it in hex instead, and converting reveals the familiar mode:
| Hex in the dump | Octal | Meaning |
|---|---|---|
81a4 |
100644 | regular file, rw-r--r-- |
41ed |
40755 | directory, rwxr-xr-x |
a1ff |
120777 | symbolic link, rwxrwxrwx |
The leading digits (10, 4, 12) are the file type bits listed in inode(7), and the last three digits are the permissions.
Quick checks and limits
fffbecomes7777. Every 12 bits of 1s give four 7s, a nice sanity check that 4-bit and 3-bit groups meet at 12.- Letters are case-insensitive:
FFandffboth give377. - No negative numbers, no fractions, no separators.
- Input above
1fffffffffffffis refused rather than rounded, because the conversion uses JavaScript numbers, which can't represent every integer past 2^53 - 1. - It runs locally in your browser.
Related tools
- Octal to hexadecimal does the reverse, for instance turning a chmod mode into the hex you'd see in a dump.
- Hex to binary shows the intermediate bit string from step 1.
- Binary to octal handles step 3 and maps each octal digit to read, write and execute.
- The ASCII table lists the hex code of every character, so you can find the byte before converting it.
Last updated