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 Hexadecimal

Octal to Hexadecimal

Find the hex twin of a legacy base-8 constant before you rewrite it, and see which constants are better left in octal exactly as they are.

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.

The digits do not map one to one: the value travels, the notation changes. 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

Anything aligned to bytes reads better in hex, where each digit covers four bits and the byte boundaries are visible.

Permissions Are the Exception

File modes group in threes, so octal stays the clearer form there — rewriting 0755 as 0x1ED makes the code harder to read, not easier.

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

1

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.

2

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.

3

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.

Auditing in reverse: put the hex constant in the lower field to recover the octal the original author wrote. Swap moves you to the hex-first page with the value carried over.

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.

OctalHexValueWhere you find it
00220x1218The default umask
06440x1A4420A plain file mode
07550x1ED493An executable or directory mode
07770x1FF511All nine permission bits set
040000x8002048The setuid bit on its own
01777770xFFFF65535A 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.

01234567
0o
0123456789ABCDEFabcdef
0x

Constants and Their Hex Twins

22 = 12
644 = 1A4
755 = 1ED
777 = 1FF
4000 = 800
177777 = FFFF

Octal (Base-8)

The notation older sources reach for, three bits to a digit and a bare leading zero as its only marker.

Hexadecimal (Base-16)

The form current style guides prefer for masks and addresses, four bits to a digit and two digits to a byte.

Never swap the prefix and keep the digits, the value changes
0644 is 420, which is 0x1A4
Leave file modes and umasks in octal, they group in threes
The copy button returns a ready-made 0x literal
Want to learn more? Read documentation →
1/5
Start typing to search...
Searching...
No results found
Try searching with different keywords