Decimal to Octal Converter
Convert base-10 to octal, and watch for the leading-zero bug it can cause in code.
Type 0755 into older JavaScript or Python 2 and it silently becomes 493, not seven hundred and fifty-five. The leading zero was not decoration — it meant “read this as octal.”
Converting 500 to octal by hand
Octal conversion uses the same repeated-division method as hex, but dividing by 8 instead of 16. 500 divided by 8 is 62, remainder 4. 62 divided by 8 is 7, remainder 6. 7 divided by 8 is 0, remainder 7.
Reading the remainders from last to first gives 764. Checking it: 7×64 + 6×8 + 4×1 = 448 + 48 + 4 = 500, so the conversion is correct.
The leading-zero trap
In C, older JavaScript and Python 2, a number literal written with a leading zero was interpreted as octal rather than decimal. Someone writing 0755 to mean “the number 755” — perhaps copying a permission value into unrelated code — would actually get 493, because 7×64 + 5×8 + 5×1 = 493.
Modern languages moved away from this. Python 3 requires an explicit 0o prefix and raises a syntax error on a bare leading zero. JavaScript’s strict mode does the same. The bug still shows up in older codebases and in anyone copying a permission string, like 0755, into a place that expects a plain integer.
Where octal numbers still appear
- File permissions — Unix chmod values such as 755 or 644, where each digit is three permission bits.
- umask — The default permission mask applied to new files, commonly written as an octal value like 022.
- Legacy numeric literals — Older code where a leading zero silently switched a number’s base, now considered a design mistake most languages have fixed.
Decimal to octal questions
What is the fastest way to convert decimal to octal by hand?
Repeated division by 8, recording the remainder at each step. Read the remainders in reverse order, from the last division to the first, to get the final octal number.
Why does 0755 not equal 755 in some programming languages?
Because a leading zero on a number literal historically told the language to interpret the rest as octal, not decimal. 0755 in octal equals 493 in decimal, a common source of confusing bugs when someone expects it to just mean seven hundred and fifty-five.
Do modern languages still have this leading-zero problem?
Mostly not. Python 3 requires an explicit 0o prefix for octal literals and rejects a bare leading zero outright, and JavaScript’s strict mode behaves the same way. The trap mainly survives in older code and scripts copied from legacy examples.
What does umask 022 actually control?
It is an octal value subtracted from default permissions when a new file or folder is created, typically removing write access for the group and everyone else. It is one of the few places octal is still set deliberately rather than inherited from old code.
Is octal ever more compact than hex or decimal?
Not generally — hex packs more value per digit since it covers 16 values per digit against octal’s 8. Octal’s advantage was never compactness; it was lining up neatly with 3-bit groupings, which matters for permissions but rarely anywhere else.