How do you convert decimal to octal?
Divide the number by 8, note the remainder, and keep dividing each quotient by 8 until you reach 0. The remainders, read in reverse order, form the octal number. Every remainder is between 0 and 7, which are the only digits base 8 uses. For example, 2024 divides down to remainders 0, 5, 7 and 3, so it is 3750 in octal.
Octal is less common than hex today, but it's still the standard notation for Unix file permissions, and that's where most people need this conversion.
Converting with the tool
- Type a whole number, like
420. - Click Convert.
- Read the result:
644.
Spaces around the number are ignored. The output never has a prefix or padding.
Worked example: 2024
| Division | Quotient | Remainder |
|---|---|---|
| 2024 ÷ 8 | 253 | 0 |
| 253 ÷ 8 | 31 | 5 |
| 31 ÷ 8 | 3 | 7 |
| 3 ÷ 8 | 0 | 3 |
Read upward: 3750. Check: 3 × 512 + 7 × 64 + 5 × 8 + 0 = 1536 + 448 + 40 = 2024.
Decoding a file mode your code printed in decimal
This is the case that sends most developers looking for a decimal to octal converter. You call fs.statSync(path).mode in Node or os.stat(path).st_mode in Python, log it, and see a number like 33188 that means nothing. File modes are designed to be read in octal, where the low three digits are the permission bits and the higher digits encode the file type (inode(7) lists S_IFREG as 0100000 and S_IFDIR as 0040000).
| Printed value | Octal | File type | Permissions |
|---|---|---|---|
| 33188 | 100644 |
regular file | rw-r--r-- |
| 16877 | 40755 |
directory | rwxr-xr-x |
| 493 | 755 |
(permissions only) | rwxr-xr-x |
| 420 | 644 |
(permissions only) | rw-r--r-- |
| 18 | 22 |
a umask of 022 | removes write for group and others |
Python can do this for you with oct(), which returns '0o100644' for 33188, and stat.filemode() turns the whole thing into -rw-r--r--. The converter is handy when you only have the number from a log line.
The leading-zero trap
The reverse mistake is just as common: writing chmod(path, 755) in code. Without an octal prefix that's decimal 755, which is octal 1363, giving a mode nobody intended. Write the literal in octal (0o755 in Python and JavaScript, 0755 in C) or pass the decimal equivalent, 493.
Languages disagree about bare leading zeros. C treats 0755 as octal. Python 3 rejects it outright as a syntax error (Python's integer literal rules) precisely to avoid this confusion.
Common conversions
| Decimal | Octal |
|---|---|
| 7 | 7 |
| 8 | 10 |
| 64 | 100 |
| 83 | 123 |
| 292 | 444 |
| 438 | 666 |
| 511 | 777 |
| 1000 | 1750 |
Notice the pattern at 8 and 64: powers of eight become 1 followed by zeros, the same way powers of ten do in decimal.
Limits
- Whole numbers from 0 to 9007199254740991 only; that maximum converts to
377777777777777777. - No negative numbers or fractions; both show "Enter a valid decimal number...".
- No grouping or padding in the output, so add a leading
0or0oyourself when you paste the result into code. - An empty input simply clears the result.
Related tools
- Binary to octal explains how each octal digit maps to a read/write/execute triplet.
- Octal to hexadecimal is useful when comparing modes against a hex dump.
- Decimal to binary shows every bit if you need to test individual permission flags.
- The Rust lessons show the
0o,0xand0binteger literal prefixes alongside Rust's integer types.
Last updated