Octal to Decimal

Convert base-8 octal numbers into ordinary decimal.

Type parseInt("010") into an old JavaScript console without a second argument and you might get 8 back, not ten. That surprise has a name: a leading zero used to mean ‘this number is octal’, and it quietly broke a lot of code before browsers changed the rule.

Octal is base 8, read the same way as decimal

Each position in an octal number is worth a power of 8 instead of a power of 10, and a single digit only ever runs from 0 to 7 — there is no 8 or 9 in octal. Take the octal value 57: the 5 sits in the eights place and the 7 sits in the ones place.

The conversion is (5 × 8) + 7 = 47. That is the same number 2F reaches in hexadecimal and 101111 reaches in binary — three different notations for the identical quantity, which is a useful thing to remember when a number looks unfamiliar.

The leading-zero bug

In the C programming language, a numeric literal that starts with a plain 0 has always been interpreted as octal — 010 in C source code means 8, not ten. JavaScript copied this behaviour informally for years, even though it was never part of the official language until it was formalised, then deprecated.

The practical fallout showed up wherever people zero-padded numbers for formatting — a date field, a ZIP code, a product code — and then ran that string through a number parser without specifying base 10 explicitly. ECMAScript 5 fixed parseInt to default to decimal, but the older behaviour still surfaces in legacy code and in languages that never changed it.

Where octal literals still show up on purpose

  • Unix file permissions — chmod and umask values are always meant as octal, which is why they are conventionally written with a leading zero or an explicit octal prefix.
  • C and C++ source code — A leading zero on an integer literal is still octal by design, a holdover from the earliest versions of the language.
  • Modern explicit prefixes — Current JavaScript, Python and most other languages require an explicit 0o prefix for octal literals, precisely to avoid the old ambiguity.

Converting octal to decimal by hand

  1. Write down each octal digit with its position, counting from the right starting at 0.
  2. Multiply each digit by 8 raised to its position.
  3. Add the results together.
  4. Double-check that no digit in your original number is 8 or 9 — if one is, it was never valid octal to begin with.

Octal to decimal questions

Why does octal use only digits 0 through 7?

Because octal is base 8, and a single digit can only represent eight distinct values. The moment you need an 8 or a 9, the number is not valid octal, the same way 2 is not a valid binary digit.

Is octal used much outside Unix permissions?

Not heavily today. Its main modern home is file permission numbers, with occasional appearances in older embedded systems, legacy file formats and C-family source code.

What caused the parseInt octal bug specifically?

Older JavaScript engines treated any numeric string starting with 0 as octal when passed to parseInt without an explicit radix argument. A value like ‘010’ silently became 8 instead of ten, which broke code that expected plain decimal parsing.

How is this different from decoding a chmod value?

It is the same underlying maths, decimal being one of several ways to interpret the same octal digits. Chmod values are usually read as permission flags instead, which is a different, non-numeric interpretation of the identical digits.

Does modern JavaScript still support octal at all?

Yes, but only with an explicit 0o prefix, such as 0o57. The old implicit leading-zero octal is deprecated and no longer works in strict mode.

Cookie
We care about your data and would love to use cookies to improve your experience.