Base64 Text Encoder
Encode text strings to Base64 for safe transmission through systems that only support ASCII characters. Essential for email, URLs, JSON payloads, and legacy systems. Our encoder processes everything locally in your browser for complete privacy.
Why Encode Text to Base64?
Base64 encoding converts any text—including special characters, Unicode, and emojis—into a safe ASCII string that can pass through any system without corruption or interpretation. Common scenarios include:
- HTTP Basic Authentication: Sending credentials as
Authorization: Basic base64(user:pass) - Email Attachments: MIME encoding for email bodies and attachments
- Data URIs: Embedding text content directly in HTML/CSS
- JSON Payloads: Including text that might break JSON parsing
- URL Parameters: Transmitting data through URL query strings
- Configuration Files: Encoding secrets in environment variables
Text to Base64 Encoding Examples
| Original Text | Base64 Encoded |
|---|---|
| Hello World | SGVsbG8gV29ybGQ= |
| user:password | dXNlcjpwYXNzd29yZA== |
| {"key":"value"} | eyJrZXkiOiJ2YWx1ZSJ9 |
| 你好世界 | 5L2g5aW95LiW55WM |
| 👋🌍 | 8J+Riw== |
JavaScript Implementation
``javascript
// Browser - Simple ASCII encoding
const base64 = btoa('Hello World');
// "SGVsbG8gV29ybGQ="
// Browser - With Unicode/emoji support (recommended)
function encodeText(text) {
const utf8Bytes = new TextEncoder().encode(text);
const binaryString = String.fromCharCode(...utf8Bytes);
return btoa(binaryString);
}
// URL-safe Base64 (for URLs and filenames)
function encodeUrlSafe(text) {
const base64 = btoa(encodeURIComponent(text).replace(
/%([0-9A-F]{2})/g,
(_, p1) => String.fromCharCode(parseInt(p1, 16))
));
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
// Node.js
const base64 = Buffer.from('Hello World').toString('base64');
// URL-safe variant
const urlSafe = Buffer.from('Hello World').toString('base64url');
`
Decoding Base64 Back to Text
`javascript
// Browser (ASCII only)
const text = atob('SGVsbG8gV29ybGQ=');
// Browser (with Unicode support)
function decodeText(base64) {
const binaryString = atob(base64);
const bytes = Uint8Array.from(binaryString, c => c.charCodeAt(0));
return new TextDecoder().decode(bytes);
}
// Node.js
const text = Buffer.from('SGVsbG8gV29ybGQ=', 'base64').toString('utf-8');
`
Common Use Cases
| Use Case | Code Example |
|---|---|
| Basic Auth Header | 'Basic ' + btoa(user + ':' + pass) |
| Data URI | 'data:text/plain;base64,' + btoa(text) |
| JSON embedding | { "data": btoa(jsonString) } |
| URL parameter | '?data=' + encodeUrlSafe(text) |
Standard vs URL-Safe Base64
| Feature | Standard | URL-Safe |
|---|---|---|
| Character 62 | + | - |
| Character 63 | / | _ |
| Padding | = (required) | Often omitted |
| Use in URLs | Needs encoding | Safe as-is |
| Filenames | Problematic | Safe |
Size Considerations
Base64 encoding increases size by approximately 33%:
3 bytes of input → 4 characters of output- 1 KB text → ~1.33 KB Base64
- 1 MB text → ~1.33 MB Base64
For large text, consider compressing (gzip) before Base64 encoding to reduce the final size.Character Set Handling
Content Type Encoding Approach ASCII only Simple btoa() works Unicode/UTF-8 Encode to bytes first Emojis Must use UTF-8 approach Mixed content Always use UTF-8
Always use UTF-8 encoding for any text that might contain non-ASCII characters to ensure correct round-trip encoding and decoding.Encoding Is Not Encryption
Base64 and percent-encoding both make data safe to *transport*. Neither makes it secret —
both are trivially reversible by design, with no key involved. A Base64 string in a URL, a
cookie or a header is readable by anyone who sees it.
Purpose Use Safe transport of binary over text Base64 Safe transport of text in a URL Percent-encoding Confidentiality AES-GCM, TLS Integrity HMAC, a digital signature Password storage bcrypt, scrypt, Argon2
Size Costs
Base64 expands data by exactly 4/3 — three bytes become four characters — plus padding. A
100 KB image becomes about 133 KB as a data URI, and it cannot be cached separately from the
document that carries it. Inline small icons; link everything else.
Percent-encoding expands unpredictably: an ASCII character that needs escaping becomes three
characters, and a non-ASCII character becomes three per UTF-8 byte. é is %C3%A9 — six
characters for one letter.
UTF-8 Is the Only Sane Default
Every encoding decision on the modern web assumes UTF-8. Where it goes wrong:
btoathrows on non-Latin-1 input.Encode to UTF-8 bytes first:
btoa(String.fromCharCode(...new TextEncoder().encode(text))).
atobreturns Latin-1.Decode back withnew TextDecoder().decode(bytes).- A BOM breaks parsers. Excel writes one at the start of CSV exports; strip it before
parsing.
Length is ambiguous."👨👩👧".lengthis 8 in JavaScript, 1 to a reader. Use
Intl.Segmenter` when the count is shown to a person.