Handling UTF-8 & Unicode in Base64: Avoiding Character Corruption
How to properly encode Arabic, Chinese, Urdu, Hindi, Cyrillic, and emojis in Base64 without Mojibake or decoding errors.
Try Toolstack's Base64 Encoder
Free, instantaneous, and processes 100% locally in your browser.
A frequent bug in legacy JavaScript applications occurs when converting text with non-ASCII characters (such as Arabic, Urdu, Japanese, or emoji like 馃殌) into Base64 using naive `btoa()` calls.
Why Naive btoa() Fails
JavaScript strings are stored internally as UTF-16 code units. The browser `btoa()` function expects each character code to fit within a single 8-bit byte (0x00 to 0xFF). When encountering a character like `馃殌` (code point U+1F680), `btoa()` immediately throws an uncaught error.
The Correct Modern Solution: TextEncoder & TextDecoder
The modern W3C standard approach converts the JavaScript string into a strict UTF-8 `Uint8Array` first: ```javascript // Safe Encoding const utf8Bytes = new TextEncoder().encode("Hello 馃實 賲乇丨亘丕"); const base64 = btoa(String.fromCharCode(...utf8Bytes)); // Safe Decoding const rawBytes = Uint8Array.from(atob(base64), c => c.charCodeAt(0)); const originalString = new TextDecoder().decode(rawBytes); ```
Toolstack's Base64 tools implement this standard natively, guaranteeing 100% accurate translation across all global languages.
More Guides in Base64 & Encoding
Explore More Free Tools on Toolstack
Client-side developer utilities, image converters, calculators, and educational engineering guides.
Bulk CSV Data Cleaner
Clean, deduplicate, filter, reorder columns, and sanitize CSV datasets 100% in browser.
Redirect Checker & GSC Analyzer
Audit HTTP redirect chains (301, 302, 308), loops, and Google Search Console exports.
Bulk URL Status Checker
Batch check HTTP status codes (200, 301, 404, 500), latency, and hops in bulk.
Time Zone Meeting Planner
Find overlapping working hours across 2 to 10 cities and copy meeting invites instantly.
Word Counter
Count words, characters with/without spaces, sentences, and paragraphs in real time with reading speed metrics.
PDF Compressor
Reduce PDF document file size directly in your browser with zero server uploads.