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)
Hexadecimal to Octal

Hexadecimal to Octal

Turn a byte you read in hex into the backslash escape that printf, tr and C string literals accept, padded to the usual three digits.

Byte Values as Backslash Escapes

Tools disagree about how to write a byte. A hex editor shows 1B, a C string literal takes \033, tr and old printf implementations want octal too, and od prints it that way by default. When you have the hex and the tool insists on base-8, this page supplies the three digits that go after the backslash.

Convert through the value, not the digits: hex groups four bits and octal groups three, so they never line up. FF is 255, and 255 in base-8 is 377 — the largest byte an escape can name.

Where Octal Escapes Still Live

Shell One-Liners

Portable scripts reach for octal because printf and tr support it everywhere, while hex escapes vary between implementations.

C String Literals

An octal escape stops after three digits, so it cannot swallow the next character the way an open-ended hex escape can.

Terminal control sequences are the everyday case: nearly every colour trick in a shell script begins with the escape character, written \033 far more often than \x1B.

Converting One Byte at a Time

1

Enter a single byte

Two hex digits at a time keeps the answer inside the 000 to 377 range that a byte escape allows. Longer input is converted happily, but the result is no longer one byte.

2

Pad the answer to three digits

Escapes are conventionally written with all three positions, so a result of 12 becomes \012. The zeros change nothing numerically but stop the next character being read as part of the escape.

3

Adjust the marker for your language

The copy button gives 0o notation, which suits Python and modern JavaScript. A C literal or a shell command needs a backslash instead, so swap the prefix as you paste.

Reading a dump the other way: put an octal byte into the lower field to get the hex a binary editor would show. Swap jumps to the octal-first page carrying the value.

Octal Escape Sequences for Single Bytes

These are the bytes that turn up most often in scripts and protocol work, written in both notations so you can match whichever one your tooling prints.

HexOctal escapeByteWhat it is
07\0077Bell, the terminal beep
0A\01210Line feed, the Unix newline
1B\03327Escape, the start of a colour sequence
41\10165Capital A in ASCII
7F\177127Delete, the last ASCII code
C2\302194A UTF-8 lead byte for Latin-1 characters

Three-Digit Spacing by Default

Octal output is grouped in threes, which happens to be exactly the width of one byte escape.

Backslashes Never Reach the Field

Paste \x1B and only the hex characters survive, so an escape copied out of source code converts without cleanup.

One Byte per Shareable Link

Add ?v=1B to the address and the page opens with that byte already translated for a note or a code review.

Escape Sequence Questions

Why can't I translate each hex digit into octal separately?

A hex digit carries four bits and an octal digit three, so the groups cross each other. Only the value survives the trip, which is why the conversion runs through the number rather than digit by digit.

How many octal digits does one byte need?

Three, and the first of them can only be 0 to 3. A byte tops out at 377, so a fourth digit means the value no longer fits in eight bits.

Should I use an octal escape or a hex one in C?

Octal has a fixed limit of three digits, while a hex escape keeps consuming valid characters — "\x1Bc" is ambiguous in a way "\033c" is not. That predictability is why old code favours base-8.

Does the 0o marker belong in a shell command?

No. That prefix is Python and JavaScript syntax. A shell wants a backslash and the digits, as in printf '\101', and chmod wants bare digits with nothing in front.

Can I convert a whole hex dump line at once?

The spaces between the bytes are stripped on entry, so the line would merge into one enormous number and the octal digits would no longer align with byte boundaries. Convert byte by byte.

0123456789ABCDEFabcdef
0x
01234567
0o

Byte Escapes in Both Bases

07 = 7
0A = 12
1B = 33
41 = 101
7F = 177
C2 = 302

Hexadecimal (Base-16)

What a binary editor, a protocol trace or a hex dump shows you: two digits standing for one byte.

Octal (Base-8)

What portable shell tools and C escapes still ask for: three digits after a backslash, never more.

Convert one byte at a time to stay inside 000 to 377
1B becomes 33, written \033 in a script
Pad to three digits so the next character stays outside the escape
Replace the copied 0o marker with a backslash for C or shell
Want to learn more? Read documentation →
1/5
Start typing to search...
Searching...
No results found
Try searching with different keywords