What is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that converts binary data into a sequence of 64 printable ASCII characters. It's one of the most widely used encoding methods in computing, essential for transmitting data over text-based protocols.
Quick Summary
- Converts binary data to 64 ASCII characters (A-Z, a-z, 0-9, +, /)
- Output is approximately 33% larger than input
- Defined in RFC 4648
- Not encryption—anyone can decode it
What is Base64?
Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string format. The name comes from the fact that it uses 64 different characters to represent data: the letters A-Z (26), a-z (26), digits 0-9 (10), and two additional characters (+ and /).
The encoding was originally designed for use in MIME (Multipurpose Internet Mail Extensions) to safely transmit binary attachments through email systems that only handled ASCII text.
How Base64 Encoding Works
Base64 works by taking groups of 3 bytes (24 bits) of binary data and splitting them into 4 groups of 6 bits each. Each 6-bit group (which can represent values 0-63) is then mapped to one of the 64 characters in the Base64 alphabet.
The Base64 Alphabet
Step-by-Step Example
Let's encode the word "Man" (a classic example from the Wikipedia Base64 article):
| Step | Data |
|---|---|
| 1. ASCII values | M=77, a=97, n=110 |
| 2. Convert to binary | 01001101 01100001 01101110 |
| 3. Regroup into 6-bit chunks | 010011 010110 000101 101110 |
| 4. Convert to decimal | 19, 22, 5, 46 |
| 5. Map to Base64 chars | T W F u |
Result: Man → TWFu
Padding with =
When the input length isn't divisible by 3, Base64 adds = padding characters to make the output a multiple of 4 characters:
Ma(2 bytes) →TWE=(one = padding)M(1 byte) →TQ==(two = padding)Man(3 bytes) →TWFu(no padding needed)
Why Use Base64?
Base64 solves a critical problem: safely transmitting binary data through text-only channels.
1. Email Attachments
The original email protocol (SMTP) was designed for 7-bit ASCII text only. Binary files like images or PDFs would be corrupted during transmission. Base64 encoding (via MIME) ensures attachments survive intact.
2. Embedding Data in Text Formats
JSON, XML, HTML, and CSS are text-based formats. To include binary data (like an image), you must convert it to text first. Base64 enables Data URIs for embedding images directly in web pages.
3. HTTP Headers and URLs
HTTP headers must be ASCII. The Authorization header for HTTP Basic Auth uses Base64 to encode credentials. For URLs, the Base64URL variant avoids problematic characters.
Common Use Cases
📧 Email Attachments
MIME encoding for sending binary files through SMTP.
🖼️ Data URIs
Embed images directly in HTML/CSS. Learn how Data URIs use Base64 →
🔐 HTTP Basic Auth
Encode username:password in Authorization headers.
🎫 JWT Tokens
JSON Web Tokens use Base64URL. Learn how JWTs use Base64URL →
📦 API Payloads
Transmit binary data in JSON API requests/responses.
🔑 Cryptographic Keys
Store keys and certificates in PEM format.
Size Overhead
Base64 encoding increases data size by approximately 33%. This is because every 3 bytes of input becomes 4 bytes of output (a 4:3 ratio).
For a 1 MB image, the Base64 encoded version will be approximately 1.33 MB. Consider this overhead when deciding whether to use Base64 for large files—sometimes a direct binary transfer is more efficient.
Base64 is NOT Encryption
If you need to protect data, use proper encryption (like AES or RSA) before optionally Base64 encoding the encrypted output for transport.
RFC Standards
Base64 encoding is defined in several RFC (Request for Comments) documents:
- RFC 4648 — The Base16, Base32, and Base64 Data Encodings (current standard)
- RFC 2045 — MIME Part One: Format of Internet Message Bodies
- RFC 4648 Section 5 — Base64URL (URL-safe variant). See our Base64URL guide.
Quick Code Examples
Here's how to encode and decode Base64 in popular languages. See our complete programming language guide for more examples.
// Encode
const encoded = btoa('Hello, World!');
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="
// Decode
const decoded = atob('SGVsbG8sIFdvcmxkIQ==');
console.log(decoded); // "Hello, World!"import base64
# Encode
encoded = base64.b64encode(b'Hello, World!').decode()
print(encoded) # "SGVsbG8sIFdvcmxkIQ=="
# Decode
decoded = base64.b64decode('SGVsbG8sIFdvcmxkIQ==').decode()
print(decoded) # "Hello, World!"Try It Yourself
Use our free Base64 encoder/decoder to convert data instantly. 100% client-side—your data never leaves your browser.
Open Encoder/Decoder →