What is Base64? — Base64 Encoding Explained Simply

What is Base64? — Base64 Encoding Explained Simply

Definition

Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-64 representation. It uses 64 printable characters (A-Z, a-z, 0-9, +, /) plus padding (=) to encode arbitrary byte sequences. Base64 is not encryption — it's encoding, meaning anyone can decode it without a key.


How Base64 Encoding Works

Base64 works by converting 3 bytes of input (24 bits) into 4 ASCII characters:

Input:   3 bytes = 24 bits
Output:  4 Base64 characters

Process:
1. Take 3 bytes → 24 bits
2. Split into 4 groups of 6 bits each
3. Map each 6-bit value (0-63) to the Base64 alphabet

Example: Encoding "Man"

Text:   M         a         n
Hex:    4D        61        6E
Binary: 01001101  01100001  01101110

6-bit:  010011  010110  000101  101110
Value:  19      22      5       46
Char:   T       W       F       u

Result: Man → TWFu

Padding

When input length isn't divisible by 3:

  • 1 byte remaining → 2 Base64 chars + == padding
  • 2 bytes remaining → 3 Base64 chars + = padding

Example: Ma → TWE=


URL-Safe Base64 (Base64url)

Standard Base64 uses + and / which need URL-encoding. Base64url replaces:

  • + → -
  • / → _
  • Removes trailing =

Used in JWT tokens, secure cookies, and URL parameters.


Common Use Cases

Data URIs in HTML/CSS

<img src="data:image/png;base64,iVBORw0KGgo...">

Email Attachments (MIME)

Email protocols require 7-bit ASCII. Base64 encodes binary attachments for safe transmission.

API Authentication

Authorization: Basic base64(username:password)

JWT Tokens

JWT's three parts (header, payload, signature) are Base64url-encoded.

Storing Binary in JSON

JSON can't represent binary directly. Base64 encodes images, files, and hashes for JSON transport.


Base64 vs Hex

Aspect Base64 Hex (Base16)
Efficiency 75% (3 ratio) 50% (1 ratio)
Output Size ~33% larger ~100% larger
Readability Low High (hex digits)
Use Case Compact encoding Human-readable

For the same binary data:

  • Hex: 2 characters per byte (e.g., 4D616E)
  • Base64: ~1.33 characters per byte (e.g., TWFu)

Base64 is ~33% more efficient than hex.


LangStop Base64 Tools