Text to Octal
Convert text into octal byte codes, the way old C strings did it.
Octal text encoding barely shows up in modern software, and the one place it genuinely survives is the octal escape sequence, a syntax C and Unix shells have supported since before most current programming languages existed.
An escape character and up to three digits
In C and in POSIX shell strings, an escape character followed by up to three octal digits represents a single byte’s value directly. Write that escape sequence followed by 102, and the string contains the character B — because octal 102 is (1 × 64) + (0 × 8) + 2 = 66, and decimal 66 is B in ASCII.
This is a genuinely old piece of syntax, present in the very first versions of C in the early 1970s, back when octal was still a routine part of how programmers thought about bytes on the hardware of the day.
Why octal escapes lost ground to hex
A hex escape needs exactly two digits to cover any byte value, 00 to FF. An octal escape can need up to three digits, and worse, a valid octal escape can accidentally swallow a following digit if you are not careful about padding it to a fixed width.
Hex aligns perfectly with byte boundaries the way octal never does, which is the same underlying reason hex became the default notation for bytes generally. Most modern style guides and linters now prefer hex or full unicode escapes, and treat octal escapes as a legacy feature to avoid in new code.
Converting text to octal
- Take the text one character at a time.
- Look up each character’s decimal code.
- Convert that decimal value to octal by repeatedly dividing by 8 and reading the remainders in reverse.
- Pad each result to three digits if you are producing escape-sequence-style output.
Where you might still meet it
- Reading older C or C++ source code that predates the common use of hex escapes.
- POSIX shell scripts and some printf implementations, where the %o format specifier still formats a number as octal text.
- Legacy embedded systems documentation written when octal was closer to standard practice.
- Old Unix manuals and file format specifications that never got updated to hex conventions.
Text to octal questions
Is text-to-octal encoding used in any current file formats?
Rarely as the primary text encoding, though POSIX tar archives still store some header fields, like file size, as octal digit strings, which is a closely related use of the same base.
How many octal digits does a single byte need?
Up to three, since three octal digits cover 0 through 511, more than enough for a byte’s 0 to 255 range, though the leading digit in that range is limited to 0 through 3.
Why would code use an octal escape instead of just typing the character?
Usually to represent a character that has no visible symbol, such as a control code, or one your keyboard and file encoding cannot type directly, without ambiguity about which byte value is meant.
Is octal escape syntax the same across C, shells and other languages?
The general idea is consistent, but the exact number of digits accepted and how the sequence is terminated can differ slightly between C, POSIX shells and other languages that support it, so it is worth checking the specific language reference.
Should I use octal or hex escapes in new code?
Hex, in almost every case. It is more compact, aligns cleanly with byte boundaries, and is what current style guides and most developers expect to see.