Base122 is an experimental binary-to-text encoding that squeezes data into UTF-8 strings more tightly than Base64. It was designed for embedding binary blobs directly inside HTML or JavaScript source, where the usual Base64 data URI wastes about a third of the bytes. This tool encodes UTF-8 text to Base122 and decodes it back, entirely in your browser.
How it works
Base122 treats the input as a continuous bit stream and reads it 7 bits at a time. Each 7-bit chunk usually maps directly to a single byte in the 0–127 range, which is one UTF-8 byte. The trick is the six values that are dangerous inside string literals: null, \n, \r, ", & and \. When a 7-bit chunk equals one of those, Base122 emits a two-byte sequence that stores which illegal value it was plus the next 7 bits of data, so the stream never contains a raw unsafe byte. A trailing “shortened” form is used when an escaped chunk is the very last piece of data.
Because most chunks cost one byte and only the six special cases cost two, the output averages roughly 1.14 bytes per 7 input bits — denser than Base64’s 1.33 bytes per 6 bits.
Why the density gain matters
The practical overhead figures are:
| Encoding | Bytes per input byte (typical) | Output vs raw |
|---|---|---|
| Base64 | ~1.33 | +33% |
| Base122 | ~1.14 | +14% |
For a 100 KB binary (a small font or icon sprite), that difference is roughly 19 KB saved. On a page where the entire goal is self-containment — no external requests — every saved kilobyte improves load time.
The reason Base122 can do this is that it packs 7 bits per output byte instead of Base64’s 6 bits. Base64’s ceiling is set by its 64-character alphabet (log₂(64) = 6 bits). Base122 uses the full 0–127 ASCII range minus the six unsafe bytes, giving it 122 usable byte values, which is why the scheme is called Base122 even though individual bytes can hold values up to 127.
When to use Base122 vs alternatives
- Base122: ideal when you are embedding a binary payload directly in a JavaScript string literal or an HTML
<script>tag in a single-file web page and want the smallest possible output without a hex-dump or Base64 cost. - Base64: use when the encoding must be portable and decodable by standard browser APIs (
atob) or any third-party tool. Base64 is the overwhelming standard for data URIs, JWTs, and email. - Hex / percent encoding: use when the bytes are low-density data (short strings, small integers) or when human readability of the encoded form matters.
Use Base122 only when you control both encoder and decoder. It is not an IETF standard and few libraries support it outside purpose-built implementations. For email or URLs, prefer Quoted-Printable or URL-safe Base64 instead, since those target those specific transports.