1. What is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that converts binary data into a set of 64 printable ASCII characters. It is not encryption — anyone can decode a Base64 string instantly. Its sole purpose is to represent binary data in environments that only support text.
The Base64 alphabet consists of the following 64 characters:
A–Z (indices 0–25)
a–z (indices 26–51)
0–9 (indices 52–61)
+ (index 62)
/ (index 63)
= (padding character)
Here is how the encoding works at the byte level:
- Take every 3 bytes (24 bits) of input data
- Split them into 4 groups of 6 bits each
- Map each 6-bit group (0–63) to a character in the Base64 alphabet
- If the input length is not a multiple of 3, pad the output with
=characters (one or two)
For example, the string Hello encodes to SGVsbG8=. Notice the single = padding because the input is 5 bytes — one byte short of a full 3-byte group.
// Encoding "Hello" step by step
H e l l o (5 bytes)
72 101 108 108 111 (ASCII decimal)
// Group into 3-byte chunks: [72,101,108] [111,0,0]
// Convert each to 24-bit, split into 6-bit groups
// Map to Base64 alphabet → "SGVsbG8="
// Padded with one "=" because we added two zero bytes
2. Why Does Base64 Exist?
Many systems were designed to handle only text — specifically printable ASCII characters. Binary data (images, executables, compressed archives) can contain any byte value from 0x00 to 0xFF, including control characters that break text protocols. Base64 solves this by converting arbitrary binary data into a safe, portable text representation.
The most common environments that require Base64:
- Email (MIME) — The original email protocols (SMTP) were designed for 7-bit ASCII text. MIME uses Base64 to attach binary files like images and PDFs to email messages.
- HTTP — While HTTP can handle binary data via Content-Type headers, some headers (like Basic Auth credentials) need to embed binary-safe text within header values.
- XML and JSON — These text-based formats have no native way to represent raw binary data. Base64 allows embedding images, certificates, and other binary blobs directly inside structured text.
- Data URLs — Browsers support embedding resources inline using the
data:URI scheme, which uses Base64 to encode the content.
The trade-off is size: Base64 encoding increases the data size by approximately 33% because every 3 bytes of input become 4 characters of output.
Try our free online Base64 Encoder/Decoder right now:
Open Base64 Tool →3. Common Use Cases
Data URLs for Images
You can embed images directly into HTML or CSS using Base64 data URLs. This eliminates an extra HTTP request, which can improve performance for small images like icons:
<!-- Inline image in HTML -->
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="red dot">
/* Inline image in CSS */
.icon {
background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0i...');
}
Note: Data URLs are best for small assets. For large images, separate files with proper caching are more efficient.
JWT Tokens
JSON Web Tokens (JWT) use Base64URL encoding (a URL-safe variant of Base64 that replaces + with -, / with _, and omits padding). A JWT has three parts separated by dots:
// JWT structure (each part is Base64URL-encoded)
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZXhwIjoxNzAwMDAwMDAwfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
// Decoded header
{"alg": "HS256"}
// Decoded payload
{"sub": "1234567890", "exp": 1700000000}
// Third part: HMAC-SHA256 signature
HTTP Basic Authentication
HTTP Basic Auth sends credentials as a Base64-encoded string in the Authorization header. The format is username:password encoded in Base64:
// Original credentials
username: admin
password: s3cret
// Combined → "admin:s3cret"
// Base64 → "YWRtaW46czNjcmV0"
// HTTP header
Authorization: Basic YWRtaW46czNjcmV0
// Fetch API example
fetch('/api/data', {
headers: {
'Authorization': 'Basic ' + btoa('admin:s3cret')
}
});
Important: Basic Auth is not encrypted — it is only encoded. Always use HTTPS to protect credentials in transit.
Email Attachments (MIME)
When you attach a file to an email, the email client encodes it using Base64. The MIME header tells the receiving client how to decode it:
Content-Type: image/png; name="photo.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="photo.png"
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk
+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==
Embedding Binary in JSON/XML
Since JSON and XML are text formats, binary data must be encoded before embedding. Base64 is the standard choice:
{
"fileName": "document.pdf",
"mimeType": "application/pdf",
"data": "JVBERi0xLjQKMSAwIG9iago8PAovVHlwZSAvQ2F0YW..."
}
Need to encode or decode Base64?
Try Encoder/Decoder Tool →4. How to Encode/Decode Online
The fastest way to work with Base64 is to use a free online tool. Here is how to use our Base64 Encoder/Decoder:
- Paste or type your text into the input area
- Click "Encode" to convert text to Base64
- Click "Decode" to convert Base64 back to the original text
- Copy the result with one click
You can also use our Encoder/Decoder Tool which supports Base64, URL encoding, HTML entities, and more — all in one place.
For developers who prefer the command line, modern browsers provide built-in functions:
// JavaScript — Browser
const encoded = btoa('Hello World'); // "SGVsbG8gV29ybGQ="
const decoded = atob('SGVsbG8gV29ybGQ='); // "Hello World"
// Python
import base64
encoded = base64.b64encode(b'Hello World') # b'SGVsbG8gV29ybGQ='
decoded = base64.b64decode(b'SGVsbG8gV29ybGQ=') # b'Hello World'
// Command line
echo -n "Hello World" | base64 # Encode
echo "SGVsbG8gV29ybGQ=" | base64 -d # Decode
5. Base64 vs URL Encoding vs Hex
There are several encoding schemes, each designed for different scenarios. Here is a comparison:
// Original string: "Hello!"
Base64: SGVsbG8h (8 chars — 33% larger than input)
URL Encoding: Hello%21 (7 chars — context-dependent size)
Hex: 48656c6c6f21 (12 chars — 100% larger)
- Base64 — Best for embedding binary data in text formats (JSON, XML, HTML). Compact but not URL-safe by default (use Base64URL variant for URLs).
- URL Encoding (Percent Encoding) — Best for encoding special characters in URLs and query parameters. Only encodes characters that are not safe for URLs.
- Hexadecimal Encoding — Best for displaying raw binary data in a human-readable way (checksums, hashes, memory addresses). Each byte becomes exactly 2 hex characters.
Use the Encoder/Decoder tool to experiment with all three formats side by side.
Ready to encode or decode?
Open Base64 Tool →