Base64 Encoding: What It Is and When to Use It

Base64 Encoding: What It Is and When to Use It

You've probably seen strings like "SGVsbG8gV29ybGQ=" in code or configuration files. That's Base64 encoding—a way to represent binary data using only printable characters. It's everywhere in web development, but often misunderstood.

What Is Base64?

Base64 is an encoding scheme that converts binary data into 64 ASCII characters: A-Z, a-z, 0-9, +, and /. The = character pads the output to ensure proper alignment.

It's not encryption—Base64 provides no security. Anyone can decode it instantly. Its purpose is safe data transport through text-only channels.

How Base64 Works

  1. Take input data (text or binary)
  2. Convert to binary (8-bit bytes)
  3. Regroup into 6-bit chunks
  4. Map each 6-bit value to a Base64 character
  5. Pad with = if needed for alignment

Example:

  • "Hi" → binary: 01001000 01101001
  • Regrouped: 010010 000110 1001(00)
  • Values: 18, 6, 36
  • Base64: "SGk="

Try it with our Base64 converter.

Why 33% Size Increase?

Base64 represents 6 bits per character instead of 8. For every 3 bytes of input, you get 4 characters of output:

  • 3 bytes × 8 bits = 24 bits
  • 24 bits ÷ 6 bits per character = 4 characters
  • 4/3 = 33% size increase

This overhead is the cost of text-safe encoding.

Common Use Cases

Email Attachments

Email was designed for text only. MIME encoding uses Base64 to send images, PDFs, and other binary files through email systems.

Data URIs

Embed small images directly in HTML or CSS:

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

Reduces HTTP requests for small assets. Convert images with our image to Base64 tool.

API Authentication

HTTP Basic Authentication encodes credentials in Base64:

Authorization: Basic dXNlcjpwYXNz

The value decodes to "user:pass"—clearly not secure without HTTPS!

JSON Web Tokens (JWT)

JWTs encode payload data in Base64URL (a URL-safe variant):

eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiam9obiJ9.signature

Storing Binary in Text Fields

Databases, configuration files, and APIs that only support text can store binary data as Base64.

Base64 Variants

Standard Base64

Uses +, /, and = characters. Can cause issues in URLs and filenames.

Base64URL

Replaces + with -, / with _, and removes padding. Safe for URLs and filenames.

MIME Base64

Adds line breaks every 76 characters for email compatibility.

When NOT to Use Base64

  • Large files: 33% overhead adds up; serve binary directly
  • Security: Base64 is encoding, not encryption
  • Compression: Encode after compressing, not before
  • All images: Only worthwhile for very small images (~1KB)

Programming Examples

JavaScript:

// Encode
btoa("Hello World");  // "SGVsbG8gV29ybGQ="

// Decode
atob("SGVsbG8gV29ybGQ=");  // "Hello World"

Python:

import base64

# Encode
base64.b64encode(b"Hello World")

# Decode
base64.b64decode("SGVsbG8gV29ybGQ=")

PHP:

// Encode
base64_encode("Hello World");

// Decode
base64_decode("SGVsbG8gV29ybGQ=");

Base64 vs. Other Encodings

  • Hex: 100% size increase vs. 33% for Base64
  • URL encoding: Variable size increase, different use case
  • UTF-8: Text encoding, not binary-to-text conversion

Use our Base64 converter for quick encoding and decoding without writing code.

Try Base64 Encoder/Decoder Now