If you've ever looked at a URL and wondered why it has %20 instead of a space, or why an image in an HTML file starts with data:image/png;base64,iVBOR... — you've already encountered URL encoding and Base64. Both transform data into ASCII-safe strings. But they exist for completely different reasons, and mixing them up causes hard-to-debug bugs. Here's the definitive guide.
What Is Encoding? (And What It Isn't)
Encoding transforms data into a different representation so it can safely travel through a medium that has restrictions. Encoding is not encryption — it doesn't protect data. Anyone can decode it. Encoding is about compatibility, not security.
Both Base64 and URL encoding answer the question: "How do I represent arbitrary data using only characters that won't break my system?"
What Is Base64?
Base64 encodes binary data (or any byte sequence) into a string of 64 printable ASCII characters: A–Z, a–z, 0–9, +, /, and = for padding.
How it works: Base64 takes every 3 bytes of input (24 bits) and splits them into 4 groups of 6 bits each. Each 6-bit group maps to one of 64 characters. The name "Base64" comes from the 64-character alphabet.
Example:
Input: Hello
Bytes: 48 65 6C 6C 6F
Base64: SGVsbG8=
Output size: Base64 output is always ~33% larger than the input. Every 3 bytes become 4 characters.
Variants:
| Variant | Characters used | Padding | Common use |
|---|---|---|---|
| Standard Base64 | A–Z a–z 0–9 + / |
= |
Email, data URIs |
| URL-safe Base64 | A–Z a–z 0–9 - _ |
Omitted or = |
JWT, URL params |
The URL-safe variant replaces + with - and / with _ to avoid conflicts with URL syntax.
What Is URL Encoding?
URL encoding (also called percent-encoding) replaces characters that are unsafe or reserved in URLs with %XX, where XX is the character's hexadecimal byte value.
Why: URLs can only contain a subset of ASCII characters. Spaces, &, =, #, and non-ASCII characters must be encoded so they don't confuse parsers.
Example:
Input: hello world & name=Alice
Encoded: hello%20world%20%26%20name%3DAlice
Reserved characters (have special meaning in URLs):
! # $ & ' ( ) * + , / : ; = ? @ [ ]
If these appear in a value (not as URL syntax), they must be percent-encoded.
Space encoding: There are two conventions:
%20— used byencodeURIComponent()and the URL specification+— used in HTML form submissions (application/x-www-form-urlencoded)
Both are widely accepted in query strings, but %20 is technically correct per RFC 3986.
Side-by-Side Comparison
| Aspect | Base64 | URL Encoding |
|---|---|---|
| Purpose | Encode binary data as text | Make text safe in URLs |
| Output characters | A–Z a–z 0–9 + / = |
%XX hex pairs + unreserved chars |
| Output size | ~133% of input | Varies (only unsafe chars encoded) |
| Reversible | Yes — fully lossless | Yes — fully lossless |
| Handles binary | Yes — designed for it | No — text only |
| Handles Unicode | Yes (encode bytes first) | Yes (UTF-8 bytes are percent-encoded) |
| URL-safe by default | No (standard variant) | Yes — that's its purpose |
When to Use Base64
1. Embedding binary data in text formats
Data URIs embed images directly in HTML or CSS:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." />
This avoids an extra HTTP request. Useful for small icons and critical above-the-fold images.
2. Email attachments (MIME)
Email is a text protocol. Attachments (PDFs, images) are Base64-encoded in the email body under Content-Transfer-Encoding: base64.
3. Binary data in JSON
JSON doesn't have a binary type. If you need to include a file, signature, or binary blob in a JSON payload, Base64 is the convention:
{
"filename": "report.pdf",
"content": "JVBERi0xLjQK..."
}
4. JWT payloads and headers
JSON Web Tokens use URL-safe Base64 (without padding) to encode their header and payload sections. This makes the token URL-safe while keeping it compact.
5. Basic HTTP authentication
The Authorization: Basic header sends username:password encoded in Base64:
Authorization: Basic dXNlcjpwYXNzd29yZA==
When to Use URL Encoding
1. Query string parameters
Any special character in a query value must be percent-encoded:
# ❌ Wrong — & splits the query string
https://example.com/search?q=cats & dogs
# ✅ Correct
https://example.com/search?q=cats%20%26%20dogs
2. HTML form submissions
Browsers automatically URL-encode form data before sending it. Understanding this is essential when building APIs that accept form data.
3. Path segments with special characters
A filename like report (final).pdf in a URL path needs encoding:
/files/report%20%28final%29.pdf
4. Encoding user-supplied values in links
When dynamically building URLs in JavaScript, always use encodeURIComponent() on values:
const query = "cats & dogs";
const url = `https://example.com/search?q=${encodeURIComponent(query)}`;
// → https://example.com/search?q=cats%20%26%20dogs
Common Mistakes
Mistake 1: Using standard Base64 in URLs
Standard Base64 uses + and /, which have special meaning in URLs. Always use URL-safe Base64 (replace + → -, / → _) when putting Base64 output in a URL or query parameter.
Mistake 2: Double-encoding
If you URL-encode an already URL-encoded string, you get %2520 instead of %20 (because % itself gets encoded to %25). This breaks decoding on the other end. Encode once, decode once.
Mistake 3: Using Base64 for security
Base64 is not encryption. dXNlcjpwYXNzd29yZA== decodes to user:password instantly. Never use Base64 to "hide" sensitive data.
Mistake 4: Forgetting UTF-8 conversion for non-ASCII
When URL-encoding a string with Unicode characters (like café), the browser first converts to UTF-8 bytes, then percent-encodes those bytes:
café → UTF-8: 63 61 66 C3 A9 → %63%61%66%C3%A9 → caf%C3%A9
encodeURIComponent() handles this automatically. Rolling your own encoding without UTF-8 handling will break for non-ASCII input.
Encode and Decode Online
Try both encodings instantly with DevZone Tools:
- Base64 Encoder/Decoder — encode text or binary to Base64 and back, with standard and URL-safe variants
- URL Encoder/Decoder — percent-encode and decode strings, supporting both
%20and+for spaces
Both tools run entirely in your browser with no data sent to a server.
Quick Reference
Use Base64 when you need to represent binary data as plain text (files in JSON, data URIs, JWT, email).
Use URL encoding when you need to put arbitrary text into a URL (query params, path segments, form values).
If you need to put Base64 output into a URL, use URL-safe Base64 — it's the best of both worlds.