Base64 Encoding of Binary Data

Understand how Base64 encodes binary data into ASCII text. Learn the 6-bit grouping process, the 64-character alphabet, padding rules, and common use cases.

Binary / Raw BytesBase64 (64 ASCII characters)Encoding

Detailed Explanation

Base64 encoding converts arbitrary binary data into a text string using 64 printable ASCII characters. It exists because many transport protocols (email, JSON, URLs) only reliably handle text, not raw binary bytes.

The Base64 alphabet:

A-Z (0-25), a-z (26-51), 0-9 (52-61), + (62), / (63), with = used for padding.

How Base64 works at the bit level:

  1. Take 3 bytes of input (24 bits total)
  2. Split into 4 groups of 6 bits each
  3. Map each 6-bit value to a character from the Base64 alphabet

Step-by-step example — encoding "Hi":

  1. ASCII values: H=72, i=105
  2. Binary: 01001000 01101001
  3. Add zero-padding to make a multiple of 6 bits: 01001000 01101001 00000000
  4. Split into 6-bit groups: 010010 000110 100100 000000
  5. Convert to Base64 indices: 18, 6, 36, 0
  6. Look up characters: S, G, k, A
  7. Since input was 2 bytes (not 3), add one = padding character

Result: "SGk="

Why 6 bits?

The number 64 = 2⁶, so 6 bits can represent values 0-63, mapping perfectly to the 64-character alphabet. Three input bytes (24 bits) divide evenly into four 6-bit groups, which is why Base64 processes data in 3-byte chunks.

The 33% size increase:

Base64 converts every 3 bytes of input into 4 characters of output, a 33% size increase. This is the unavoidable cost of representing binary data as text.

Padding rules:

  • 3 input bytes -> 4 Base64 characters (no padding)
  • 2 input bytes -> 3 Base64 characters + =
  • 1 input byte -> 2 Base64 characters + ==

URL-safe Base64: Replaces + with - and / with _ to avoid conflicts with URL special characters. Used in JWTs, data URIs, and URL parameters.

Use Case

Backend developers use Base64 encoding to embed binary assets like images, certificates, or cryptographic keys within JSON API responses, configuration files, and environment variables.

Try It — Number Base Converter

Open full tool