← Home · Blog

Understanding Base64 Encoding for Developers

Published: May 30, 2025 · 5 min read

You've probably seen Base64 strings — those long sequences of letters, numbers, and sometimes +, /, and = characters. They appear in data URIs, API tokens, email attachments, and JWT payloads. But what exactly is Base64, and why do we need it?

The Problem Base64 Solves

Many protocols and formats (HTTP headers, JSON, URLs, email) only support text characters. But sometimes you need to transmit binary data (images, files, encrypted bytes) through these text-only channels. Base64 converts any binary data into a safe set of 64 ASCII characters that won't break any text-based system.

How It Works Internally

The encoding process:

  1. Take 3 bytes (24 bits) of input data
  2. Split into 4 groups of 6 bits each
  3. Map each 6-bit value to one of 64 characters: A-Z (0-25), a-z (26-51), 0-9 (52-61), + (62), / (63)
  4. If input isn't divisible by 3, add = padding characters

Example: The text "Hi" (2 bytes: 72, 105) becomes "SGk=" in Base64.

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

The Size Overhead

Base64 always increases data size by approximately 33%. Three bytes of input become four bytes of output. A 1MB image becomes ~1.33MB when Base64-encoded. This is the trade-off for text-safe transmission.

Base64 vs Base64URL

Standard Base64 uses + and / which are special characters in URLs. Base64URL replaces them:

VariantCharacter 62Character 63PaddingUsed In
Base64+/=Email (MIME), data URIs
Base64URL-_OptionalJWTs, URL parameters

Common Uses in Web Development

  • Data URIs: Embedding small images directly in HTML/CSS
    background: url(data:image/png;base64,iVBORw0KGgo...)
  • HTTP Basic Auth: Username:password is Base64-encoded
    Authorization: Basic dXNlcjpwYXNz
  • JWT Tokens: Header and payload are Base64URL-encoded
  • API Payloads: Sending binary files (PDFs, images) within JSON
  • Email Attachments: MIME encoding uses Base64 for binary attachments
  • Cryptography: Encrypted data and keys are often stored as Base64 strings

Encoding in Different Languages

// JavaScript (browser) btoa("Hello World") // Encode: "SGVsbG8gV29ybGQ=" atob("SGVsbG8gV29ybGQ=") // Decode: "Hello World" // JavaScript (Node.js) Buffer.from("Hello").toString('base64') Buffer.from("SGVsbG8=", 'base64').toString() # Python import base64 base64.b64encode(b"Hello").decode() # "SGVsbG8=" base64.b64decode("SGVsbG8=").decode() # "Hello"

Common Pitfalls

  • Base64 is NOT encryption — anyone can decode it. Never use it to "hide" sensitive data
  • Don't Base64 large files — the 33% size increase adds up. Use proper file uploads instead
  • Watch for line breaks — some implementations add line breaks every 76 characters (MIME format). Strip them before decoding
  • Unicode handling — JavaScript's btoa() only handles ASCII. For UTF-8, use btoa(unescape(encodeURIComponent(text)))

Try It

Need to encode or decode Base64 quickly? Use our free Base64 Encoder/Decoder — paste text or Base64, click convert, done. Supports UTF-8 and handles the Unicode edge cases automatically.

More Articles

What is JWT — Token Authentication

What is JSON — Simple Explanation

JSON vs XML vs YAML

← All Articles · Home · Privacy Policy