Retiring Octal Constants from Old Source
Inherited code is full of base-8: masks written as 0177, buffer sizes as 01000, terminal characters as \033. Modern style guides prefer hex almost everywhere, because it lines up with bytes and with the way debuggers print. Before you rewrite a constant, you need its hex twin — and you need to be sure the value did not shift on the way.
0644 is 420, and 420 in base-16 is 1A4. Three octal digits became three hex digits here, but that is a coincidence, not a rule.When the Rewrite Is Worth It
Masks and Field Widths
Permissions Are the Exception
Seeing both forms side by side is also the fastest way to review someone else's migration: if the hex value does not match the octal one, the change altered behaviour.
Rewriting a Constant Safely
Copy the constant out of the source
Type the digits with or without their leading zero; only 0 to 7 are accepted, so a value that is not really octal will show itself immediately.
Check the hex against the width
Results appear in pairs of digits. If a constant that should describe one byte comes back as three characters, it was never a byte value to begin with.
Paste the new literal in
Copying the hex field gives you 0x and the digits with no spaces, which is the literal form C, Rust, Python and JavaScript all share.
Legacy Octal Constants and Their Hex Twins
These are the constants that turn up most often in older C and shell sources, with the value they share so you can confirm a rewrite changed nothing.
| Octal | Hex | Value | Where you find it |
|---|---|---|---|
0022 | 0x12 | 18 | The default umask |
0644 | 0x1A4 | 420 | A plain file mode |
0755 | 0x1ED | 493 | An executable or directory mode |
0777 | 0x1FF | 511 | All nine permission bits set |
04000 | 0x800 | 2048 | The setuid bit on its own |
0177777 | 0xFFFF | 65535 | A full sixteen-bit word |
Results Grouped as Bytes
Hex output is spaced in pairs, so you can see at a glance whether a converted constant fills one byte, two or more.
Capitalised for Constants
Letters come back as A to F, the casing most C and Rust style guides ask for in a numeric literal.
Clipboard Ready for the Patch
The copy button prefixes the digits with 0x and strips the display spacing, so the replacement literal is complete.
Migration Questions
Why isn't 0777 simply written 0x777?
Because the digits mean different weights. 0777 is 511, while 0x777 is 1911 — a completely different number. Copying the digits across and swapping the prefix is the single most common mistake in this kind of cleanup.
Which constants should stay in base-8?
Anything that groups in threes: file modes, umasks, and the permission arguments of system calls. Their digits map onto rwx triplets, and hex would scatter those triplets across nibble boundaries.
Do three octal digits always become three hex digits?
No. Three octal digits hold nine bits and three hex digits hold twelve, so the count often shrinks: 0377 becomes the two-character 0xFF. Judge the width by the value, never by the digit count.
Will the compiler warn me if I get it wrong?
Rarely, because both spellings are legal numbers. The change only shows up at runtime, which is why converting the value first and pasting the exact result is worth the extra few seconds.
How do I move an escape such as \033 into hex?
Convert the digits, then rebuild the escape by hand: 33 in base-8 is 1B, so the sequence becomes \x1B. Watch what follows it, since a hex escape keeps reading valid characters while an octal one stops after three.
No comments yet. Be the first to comment!