Back to Blog

Base64 Encoding Explained for Developers

February 18, 2025 · 7 min read

What is Base64?

Base64 is a binary-to-text encoding scheme that represents binary data as a sequence of printable ASCII characters. It converts every 3 bytes of binary data into 4 ASCII characters using a set of 64 characters: uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), plus (+), and slash (/), with = used for padding.

How Does Base64 Work?

The encoding process follows these steps:

  1. Take the input data and convert it to its binary representation.
  2. Split the binary data into groups of 6 bits (since 2⁶ = 64 possible values).
  3. Map each 6-bit group to the corresponding character in the Base64 alphabet.
  4. If the final group has fewer than 6 bits, pad with zeros and add = characters.

Example: Encoding "Hi"

Text:    H         i
ASCII:   72        105
Binary:  01001000  01101001
6-bit:   010010  000110  1001(00)  ← padded
Base64:  S       G       k       =

Result: "SGk="

When to Use Base64

1. Embedding Images in HTML/CSS (Data URIs)

One of the most common uses is embedding small images directly in HTML or CSS using data URIs. This eliminates an extra HTTP request, which can improve performance for small icons and sprites:

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

/* Or in CSS */
.icon {
  background-image: url('data:image/svg+xml;base64,PH...');
}

2. Email Attachments (MIME)

Email protocols (SMTP) were designed for text-only transmission. Base64 is the standard encoding used by MIME (Multipurpose Internet Mail Extensions) to safely embed binary attachments like images, PDFs, and documents within text-based email messages.

3. API Data Transfer

When you need to send binary data (like file uploads) through JSON APIs, Base64 encoding is often used since JSON only supports text strings. This is common in REST APIs that accept file uploads as Base64 strings in the request body.

4. Storing Binary Data in Text Formats

Databases that only support text fields, configuration files (like YAML or JSON), and other text-based storage systems often use Base64 to store binary blobs like encryption keys or certificates.

When NOT to Use Base64

  • Large files: Base64 increases data size by ~33%. For large files, use multipart form uploads instead.
  • Encryption: Base64 is not encryption. It is trivially reversible and provides zero security. Never use it to "hide" sensitive data.
  • Performance-critical paths: Encoding/decoding adds CPU overhead. For binary protocols, send raw bytes.

Base64 in JavaScript

Modern browsers provide built-in functions for Base64 encoding and decoding:

// Encoding
const encoded = btoa("Hello, World!");
// Result: "SGVsbG8sIFdvcmxkIQ=="

// Decoding
const decoded = atob("SGVsbG8sIFdvcmxkIQ==");
// Result: "Hello, World!"

// For Unicode support (Node.js / modern browsers)
const utf8Encoded = btoa(unescape(encodeURIComponent("Hello 🌍")));

Base64 URL-Safe Variant

Standard Base64 uses + and / characters, which conflict with URL syntax. The URL-safe variant (Base64url) replaces these with - and _, and omits the = padding. This is used extensively in JWT tokens and OAuth.

Try it yourself

Encode and decode Base64 strings instantly in your browser.

Open Tool