Skip to content
Why Base64?
step 1/5

Reading — step 1 of 5

Read

~1 min readThe Basics

Why Base64?

Email, URLs, JSON, XML — many text protocols can't safely transmit arbitrary bytes. Bytes might:

  • Break the parser (e.g. quote chars in JSON strings)
  • Get corrupted by 7-bit transmissions (early SMTP)
  • Be misinterpreted as control characters

Base64 encodes binary as 64 ASCII printable chars. Output uses [A-Za-z0-9+/], with = for padding.

"Hello"  (5 bytes: 0x48, 0x65, 0x6c, 0x6c, 0x6f)
  -> "SGVsbG8="

Trade-off: 4 bytes of base64 represent 3 bytes of input. 33% size overhead.

Used in:

  • Email attachments (MIME)
  • Data URIs (data:image/png;base64,...)
  • HTTP Basic auth (Authorization: Basic <b64>)
  • JSON Web Tokens (header.payload.sig)
  • Certificate files (PEM format)
  • SVG <image href="data:..." />

When NOT to use base64:

  • Internal binary protocols (use raw bytes)
  • Storage where size matters

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…