Encoders & Decoders4 min readUpdated 2026-11-18

Base64 Explained: How Binary Text Encoding Works

How 3-byte blocks become 4 ASCII characters, padding rules, and URL-safe Base64.

Text protocols like JSON, email headers, HTTP query strings, and XML expect printable characters. Binary bytes—such as PNG images, cryptographic signatures, or compressed buffers—contain raw values that break text parsers or get mangled by line-ending conversions. Base64 solves transport safety by translating arbitrary bytes into 64 universally safe ASCII characters.

Base64 is encoding, never encryption

Anyone can reverse a Base64 string in milliseconds with zero key. Never treat Base64 as a security control for API keys, personal data, or passwords.

Step-by-step: how 3 bytes become 4 characters

Defined in RFC 4648, standard Base64 groups raw input 3 bytes (24 bits) at a time. Because 24 divides cleanly by 6, those 24 bits split into four 6-bit numbers (each ranging from 0 to 63). Each 6-bit number maps directly onto an alphabet of 64 printable characters: A–Z (0–25), a–z (26–51), 0–9 (52–61), plus + (62) and / (63).

Worked Example: Encoding ASCII "Man" (RFC 4648 Section 10)
Text:       M         a         n
ASCII dec:  77        97        110
8-bit bin:  01001101  01100001  01101110   (24 bits total)
6-bit grp:  010011 | 010110 | 000101 | 101110
Decimal:    19       22       5        46
Base64:     T        W        F        u   -> "TWFu"

What happens when input length is not divisible by 3? If two bytes remain (16 bits), the encoder pads two zero bits on the right to form three 6-bit characters and appends one = character (so Ma becomes TWE=). If only one byte remains (8 bits), it pads four zero bits to output two characters plus == (so M becomes TQ==). Padding guarantees the encoded length is a clean multiple of four.

Why Base64 makes files roughly 33% larger

Turning every 3 bytes into 4 ASCII bytes increases byte size by 4 / 3 ≈ 1.333× (a 33% overhead), plus up to two padding bytes or optional line wraps every 76 characters in MIME emails. Because Base64 outputs plain text, HTTP compression (gzip or Brotli) shrinks repetitive ASCII back down somewhat on the wire, yet the browser still parses and caches the larger string in CSS or HTML.

Standard Base64 vs. Base64url (JWTs and URLs)

Notice the last two characters in standard Base64: +, /, and =. Every one of those three characters carries special meaning in URLs (+ decodes as a space in query strings, / separates path segments, and = separates parameter key/value pairs). Base64url (RFC 4648 Section 5) swaps + for -, / for _, and usually omits trailing = padding.

Comparing Base64 variants across real developer workflows
VariantCharacters 62 & 63PaddingCommon Usage
Standard Base64+ and /= or == requiredData URIs, Basic Auth headers, JSON blobs
Base64url- and _ (hyphen, underscore)Often omittedJWT headers/payloads, OAuth PKCE, URL tokens
MIME Base64+ and /= or ==Email attachments (wrapped at 76 columns)

If a decoder fails on a token copied from an address bar or Authorization: Bearer header, check whether it uses - and _. Our Base64 Decoder and JWT Decoder accept URL-safe strings automatically, while URL Encode / Decode handles percent-encoding when standard Base64 must stay untouched.

When Base64 helps—and when to skip it

  • Good fit — HTTP Basic Auth & cryptographic tokens: Headers and JSON objects expect ASCII text; encoding username:password or raw Ed25519 signatures makes payloads transport-safe.
  • Good fit — tiny inline icons (under ~2 KB): Embedding a 400-byte SVG or favicon as a Data URI saves an extra HTTP round trip. Use our Base64 Encoder to generate clean image Data URIs locally.
  • Poor fit — large photos or PDFs in HTML/JSON: Inlining a 600 KB photo bloats HTML to 800 KB of un-cacheable text, blocks initial page parsing, and bypasses browser image caching. Serve large assets as separate files whenever possible.

UTF-8 multi-byte text and Data URI anatomy

A frequent bug in browser code comes from passing Unicode strings directly into btoa(). Native window.btoa() expects single-byte Latin-1 values (0–255) and throws InvalidCharacterError on accented characters above U+00FF, CJK text, or emoji. Reliable UTF-8 Base64 encoders convert text to a Uint8Array via TextEncoder first, encode those raw UTF-8 bytes cleanly, and decode them back via TextDecoder on the receiving end.

When you embed images or fonts via Data URIs, always check the prefix: data:image/png;base64,iVBORw0KGgo... combines a MIME media type with the ;base64, marker before the payload. Stripping that prefix before running Base64 Decode prevents invalid-character errors on the : and ; metadata separators.

Try the FormatFlow Tools Mentioned Above

All tools run privately in browser memory with zero file uploads.