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 WorldSGVsbG8gV29ybGQ= user:passworddXNlcjpwYXNzd29yZA== {"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
FeatureStandardURL-Safe Character 63/_ Padding= (required)Often omitted Use in URLsNeeds encodingSafe 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
Character Set Handling
| Content Type | Encoding Approach | ASCII onlySimple btoa() works Unicode/UTF-8Encode to bytes first EmojisMust 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.