Language
English English Vietnamese (Tiếng Việt) Vietnamese (Tiếng Việt) Chinese (简体中文) Chinese (简体中文) Portuguese (Brazil) (Português do Brasil) Portuguese (Brazil) (Português do Brasil) Spanish (Español) Spanish (Español) Indonesian (Bahasa Indonesia) Indonesian (Bahasa Indonesia)
Octal to Decimal

Octal to Decimal

Find out what a zero-padded literal is really worth before it reaches a comparison, with the language rules that decide when 011 stops being eleven.

What a Leading Zero Really Costs You

A number written with a zero in front is not the number you think it is. In C, in Java, in Go and in pre-2015 JavaScript, 011 is a base-8 literal worth nine. Configuration files, CSV imports and hand-written test data are full of zero-padded values, and every one of them is a small trap. Paste the suspicious literal here and see what the compiler will actually store.

Place values are powers of eight: 1, 8, 64, 512 and so on. That makes 0755 equal to 7 × 64 + 5 × 8 + 5, which is 493 — not the seven hundred and fifty-five you might read at a glance.

How the Bug Usually Arrives

Zero-Padded Data

Product codes, dates and account numbers are padded for alignment; pasted into source they quietly change base.

Comparisons That Never Match

A permission check written as mode == 644 compares against 644 decimal and fails against the 420 the system holds.

Knowing the decimal value also settles the opposite argument: if a literal is meant to be octal, seeing the base-10 number confirms it lands where you expect.

Testing a Suspicious Literal

1

Drop in the digits as written

Leading zeros are harmless here and any 0o marker loses its letter on the way in, so a Python literal can be pasted as it stands.

2

Compare with what you expected

If the number below differs from the digits you typed, the value is genuinely being read in base-8 and any comparison against a decimal constant is wrong.

3

Put the real value in the fix

The decimal field copies as a bare number with no marker, which is what a corrected comparison or a test assertion needs.

Checking the intended literal: type the decimal value into the lower field and the octal form appears above, so you can see which digits the source should have carried. Swap opens the decimal-first page with your value.

Leading Zeros in Source Code

The same characters mean different things depending on the language and the notation. This is what each literal is actually worth once it has been parsed.

Written asRead asDecimal valueNote
010Octal8C, Java, Go and legacy JavaScript
011Octal9The classic off-by-two surprise
0o10Octal8Explicit form in Python 3 and modern JavaScript
0644Octal420A file mode, the number a script prints
0755Octal493An executable mode
01000Octal512Eight cubed, not a thousand

Invalid Digits Cannot Sneak In

Typing 8 or 9 does nothing, the same refusal a compiler gives when a literal such as 09 turns out not to be legal octal.

The Gap Appears as You Type

Each keystroke updates the value, so you can watch the distance between what is written and what is stored grow digit by digit.

Evidence for the Code Review

A ?v= link opens with the literal already converted, which makes the point faster than a paragraph of explanation.

Questions About Octal Literals

Why does printing 011 in C give me 9?

The leading zero told the compiler the literal was base-8, so the digits mean one eight plus one. The variable holds nine and prints as nine; nothing is lost, it simply never was eleven.

What happened to this notation in Python 3?

It was removed on purpose. Writing 0644 is now a syntax error and you must write 0o644 instead, which makes the base impossible to miss when reading the code.

Is 09 a valid literal anywhere?

Not as octal — 9 is outside the digit range, so C rejects it and strict-mode JavaScript refuses the whole legacy form. Sloppier parsers may fall back to decimal, which is worse, because the meaning then depends on the tool.

Can a JSON file carry an octal number?

No. The format forbids a leading zero altogether, so a mode has to travel as a decimal integer or as a quoted string. That is exactly why a permissions field so often arrives as 420.

Does a zero-padded string behave the same way?

A quoted "011" stays text until something parses it, and most modern parsers then read it as decimal eleven. The danger is the unquoted literal in source, where the base is decided at compile time.

01234567
0o
0123456789

Literals and Their Real Values

010 = 8
011 = 9
017 = 15
0644 = 420
0755 = 493
01000 = 512

Octal (Base-8)

What a leading zero silently selects in several languages, with place values of 1, 8, 64 and 512.

Decimal (Base-10)

The value your comparison, assertion or log statement is really working with once the literal has been parsed.

Paste the literal with its zeros, they change nothing here
0755 is 493, not seven hundred and fifty-five
8 and 9 are refused, exactly as a compiler refuses them
The decimal field copies bare, ready for a corrected comparison
Want to learn more? Read documentation →
1/5
Start typing to search...
Searching...
No results found
Try searching with different keywords