What is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 printable ASCII characters. It was designed to allow binary data to be transmitted through systems that only support text, such as email protocols and HTML documents.
The name "Base64" comes from the 64 characters used in the encoding: uppercase A-Z (26), lowercase a-z (26), digits 0-9 (10), and two additional characters (typically + and /).
How Base64 Works
The encoding process converts every 3 bytes (24 bits) of input into 4 characters (6 bits each = 24 bits) of output:
- Take 3 input bytes (24 bits)
- Split into 4 groups of 6 bits each
- Convert each 6-bit group to its corresponding Base64 character
- If input isn't divisible by 3, add padding (=)
Example: "Man"
M = 77 = 01001101
a = 97 = 01100001
n = 110 = 01101110
Combined: 010011 010110 000101 101110
T W F u
"Man" → "TWFu"Common Use Cases
Embedding Images in HTML/CSS
Small images can be embedded directly in HTML or CSS using Data URIs:
<img src="data:image/png;base64,iVBORw0KGgo..." />
.icon {
background: url(data:image/svg+xml;base64,PHN2ZyB4...);
}Email Attachments (MIME)
Email protocols like SMTP were designed for 7-bit ASCII text. Base64 encoding allows binary attachments (images, documents) to be transmitted safely through these text-only systems.
Storing Binary in JSON/XML
JSON and XML don't natively support binary data. Base64 encoding allows you to include binary data as a text string within these formats.
API Authentication
HTTP Basic Authentication encodes credentials (username:password) in Base64:
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
// Decodes to: username:passwordBase64 Variants
Standard Base64 (RFC 4648)
Uses A-Z, a-z, 0-9, +, /, and = for padding. This is the most common variant used for email and general purposes.
URL-Safe Base64
Replaces + with - and / with _ to avoid URL encoding issues. The padding (=) is often omitted. Used in JWTs, URL parameters, and filenames.
MIME Base64
Same as standard but adds line breaks every 76 characters. Used in email encoding to comply with SMTP line length limits.
Important Considerations
Size Increase
Base64 encoding increases data size by approximately 33%. For every 3 bytes of input, you get 4 bytes of output. Consider this overhead when encoding large files.
Not Encryption
Base64 is NOT encryption—it provides no security. Anyone can decode Base64 data. Never use it to "protect" sensitive information. Use proper encryption (AES, RSA) for security.
Character Encoding
When encoding text, be aware of character encoding (UTF-8, UTF-16, etc.). This tool uses UTF-8, which is the most common encoding for web applications. Decoding with the wrong character set can produce garbled output.
Base64 in JavaScript
// Encode (ASCII only)
btoa("Hello"); // "SGVsbG8="
// Decode
atob("SGVsbG8="); // "Hello"
// For Unicode text
function encodeUnicode(str) {
return btoa(encodeURIComponent(str)
.replace(/%([0-9A-F]{2})/g, (_, p1) =>
String.fromCharCode('0x' + p1)));
}
function decodeUnicode(str) {
return decodeURIComponent(atob(str)
.split('').map(c =>
'%' + c.charCodeAt(0).toString(16).padStart(2, '0')
).join(''));
}