What is Base64 Encoding? A Complete Guide
Table of Contents
What is Base64?
Base64 is a binary-to-text encoding scheme that converts binary data into a string of ASCII characters. It uses 64 different characters (A-Z, a-z, 0-9, +, /) to represent data, making it safe to transmit over text-based protocols.
The name "Base64" comes from the fact that it uses 64 unique characters to represent data. Each character represents 6 bits of the original data (2^6 = 64).
How Base64 Works
Base64 encoding works by:
- Taking 3 bytes (24 bits) of binary data
- Splitting them into 4 groups of 6 bits each
- Converting each 6-bit group to its corresponding Base64 character
If the input isn't divisible by 3 bytes, padding characters (=) are added to make the output length a multiple of 4.
Common Use Cases
- Email attachments - MIME encoding uses Base64 to send binary files
- Data URLs - Embedding images directly in HTML/CSS
- API authentication - HTTP Basic Auth uses Base64
- Storing binary in JSON - JSON doesn't support binary, so Base64 is used
- URL-safe data - Transmitting binary data in URLs
Code Examples
JavaScript
// Encode
const encoded = btoa("Hello World");
// Result: "SGVsbG8gV29ybGQ="
// Decode
const decoded = atob("SGVsbG8gV29ybGQ=");
// Result: "Hello World"
Python
import base64
# Encode
encoded = base64.b64encode(b"Hello World")
# Result: b'SGVsbG8gV29ybGQ='
# Decode
decoded = base64.b64decode(encoded)
# Result: b'Hello World'
Try our free Base64 encoder/decoder tool
Open Base64 Tool →Key Takeaways
- Base64 increases data size by ~33% (4 output chars for every 3 input bytes)
- It's encoding, not encryption - anyone can decode it
- Use URL-safe Base64 (
-and_instead of+and/) for URLs - Great for text-based protocols, but avoid for large files