Back to Blog
base64 2025-02-10

Base64 Encoding Explained: How It Works and When to Use It

A thorough explanation of Base64 encoding, the algorithm behind it, and practical use cases in web development.

Base64 encoding is one of the most fundamental encoding schemes in computing. Whether you are embedding images in emails, storing binary data in JSON, or handling authentication tokens, Base64 is everywhere.

What Exactly Is Base64?

Base64 is a binary-to-text encoding scheme that converts binary data into a string of 64 printable ASCII characters. The name "Base64" comes from the fact that it uses a 64-character alphabet.

The Base64 Alphabet

The standard Base64 alphabet consists of:

  • A-Z (indices 0-25)
  • a-z (indices 26-51)
  • 0-9 (indices 52-61)
  • + (index 62)
  • / (index 63)
  • = (padding character)

Step-by-Step Encoding Process

Let's encode the string "Hi!" step by step:

1. Convert to binary: H=01001000, i=01101001, !=00100001

2. Concatenate: 010010000110100100100001

3. Split into 6-bit groups: 010010 000110 100100 100001

4. Convert to decimal: 18, 6, 36, 33

5. Map to Base64 alphabet: S, G, k, h

6. Result: SGkh

Text:    H        i        !

ASCII: 72 105 33

Binary: 01001000 01101001 00100001

6-bit: 010010 000110 100100 100001

Base64: S G k h

Padding Explained

When the input length is not divisible by 3, padding with = is added:

  • 1 byte remaining: 2 Base64 chars + ==
  • 2 bytes remaining: 3 Base64 chars + =
  • 0 bytes remaining: no padding needed

URL-Safe Base64

Standard Base64 uses + and / which have special meanings in URLs. URL-safe Base64 (Base64url) replaces:

  • + with -
  • / with _
  • Padding = is often omitted

This variant is specified in RFC 4648 and is used in JWT tokens and other URL-sensitive contexts.

Common Use Cases

Data URIs in HTML/CSS

embedded image

Email Attachments (MIME)

Email protocols like SMTP only support 7-bit ASCII. Base64 allows binary files to be transmitted safely.

HTTP Basic Authentication

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

// This decodes to "username:password"

Storing Binary in JSON/XML

Since JSON and XML are text-based, binary data must be encoded as text.

Size Overhead

Base64 encoding increases data size by approximately 33%. Every 3 bytes of input produces 4 bytes of output. This overhead is important to consider when:

  • Embedding large images in HTML
  • Storing binary data in databases as text
  • Transmitting large payloads over APIs

Base64 in Different Languages

// JavaScript

btoa("Hello"); // "SGVsbG8="

atob("SGVsbG8="); // "Hello"

Python

import base64

base64.b64encode(b"Hello") # b'SGVsbG8='

base64.b64decode(b"SGVsbG8=") # b'Hello'

Important Reminders

  • Base64 is encoding, not encryption. It provides no security.
  • Always consider the 33% size increase
  • Use URL-safe variant when Base64 strings appear in URLs
  • Modern browsers support native Base64 operations via btoa() and atob()

Try our Base64 Encoder/Decoder tool for instant encoding and decoding.