What is Base64? Encoding Explained with Examples

Why do email and web use Base64? From the rationale for handling binary as text to real implementation examples.

What is Base64?

Base64 is an encoding scheme that represents binary data using 64 ASCII characters.

Why Base64?

Text-based protocols (email, XML, etc.) cannot natively handle binary data. Base64 converts binary into a safe, text-only format.

How It Works

Input bytes are split into 6-bit groups, each mapped to one of 64 characters (A-Z, a-z, 0-9, +, /).

Input: "Man"
Binary:   01001101 01100001 01101110
6-bit:    010011 010110 000101 101110
Base64:   T      W      F      u

Common Use Cases

  • Email attachments: MIME encoding
  • Data URIs: <img src="data:image/png;base64,...">
  • JSON/REST APIs: embedding binary within JSON
  • Auth headers: HTTP Basic Authentication

JavaScript

// Encode
const encoded = btoa('Hello, World!');
// "SGVsbG8sIFdvcmxkIQ=="

// Decode
const decoded = atob('SGVsbG8sIFdvcmxkIQ==');
// "Hello, World!"

Important Notes

  • Base64 is not encryption — it can be decoded by anyone
  • It increases data size by ~33%
  • For URLs, use URL-safe Base64: replace + with - and / with _