Why btoa("안녕하세요") fails
JavaScript strings are not raw UTF-8 bytes. btoa() does not automatically encode a string as UTF-8: characters outside the byte range fail, and some characters inside that range may produce bytes that are not the UTF-8 representation you intended. Use TextEncoder for multilingual text, including accents, rather than relying on whether a particular call happens to avoid an exception.
A Unicode-safe encoder and decoder
The code below converts text to bytes explicitly. The encoder processes bytes in chunks so a large spread operation does not exceed the JavaScript argument limit. The decoder accepts Base64URL characters and whitespace, validates the format, restores missing padding, and uses a fatal UTF-8 decoder so invalid bytes produce an error rather than replacement characters. This example is for text, not a file preview.
Browser code and Node.js are different environments
In Node.js, Buffer.from(text, "utf8").toString("base64") is a standard way to encode UTF-8 text. Buffer decoding can be permissive, so do not treat successful conversion alone as validation of untrusted input. In a browser, the TextEncoder/TextDecoder approach below avoids depending on Node’s Buffer API. Decodex supports UTF-8 text up to 5 MB and performs conversion locally.
Verified experiments
| Input / operation | Expected result | Browser result |
|---|---|---|
btoa("한글") | Error (expected rejection) | Not run |
btoa("é") | 6Q== | Not run |
é · TextEncoder | w6k= | Not run |
é / e + U+0301 | w6k= / ZcyB | Not run |
100000 × a | 100000 → 133336 → 100000 | Not run |
What the results mean
btoa("é") succeeds but encodes a Latin-1 byte rather than UTF-8. Visually identical composed and decomposed accents also produce different bytes. The large fixture verifies exact text equality, not just output length.
How to reproduce
Run the checks, then copy the code example into a browser console. Compare btoa("é") with utf8ToBase64("é"). Try both accents without normalization; normalize only if your receiving protocol requires it.
// Browser JavaScript: text → UTF-8 bytes → Base64.
function utf8ToBase64(text) {
const bytes = new TextEncoder().encode(text);
let binary = '';
for (let i = 0; i < bytes.length; i += 8192) {
binary += String.fromCharCode(...bytes.subarray(i, i + 8192));
}
return btoa(binary);
}
// Accept standard Base64 or Base64URL, with complete or omitted padding.
function base64ToUtf8(value) {
let normalized = value.replace(/\s/g, '').replace(/-/g, '+').replace(/_/g, '/');
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(normalized) ||
normalized.length % 4 === 1 ||
(normalized.includes('=') && normalized.length % 4 !== 0)) {
throw new Error('Invalid Base64 characters, length or padding');
}
normalized += '='.repeat((4 - normalized.length % 4) % 4);
const binary = atob(normalized);
const bytes = Uint8Array.from(binary, character => character.charCodeAt(0));
return new TextDecoder('utf-8', { fatal: true }).decode(bytes);
}
const text = '안녕하세요 😀';
const encoded = utf8ToBase64(text);
console.log(encoded); // 7JWI64WV7ZWY7IS47JqUIPCfmIA=
console.log(base64ToUtf8(encoded)); // 안녕하세요 😀
Verification method
These fixed, public fixtures run against the same file codec and character codecs used by the converter. The JavaScript cases additionally check browser-native btoa, TextEncoder, URLSearchParams and SHA-256. Expected outputs are published before any script runs; the button shows actual results in your browser. No input is uploaded.
JavaScript · guide-cases.js · JavaScript · file-codec.js
Scope and limitations
Checked on 2026-10-21. This is a correctness check with small fixtures and one 100,000-character case, not a speed benchmark or a guarantee for every file. The four-byte file is synthetic binary data, not a PNG or PDF. SHA-256 equality checks byte preservation; it does not establish that a file is safe to open. Results can depend on browser support.
References
Choose a guide for your problem
How to fix Base64 decoding errors
Diagnose which layer failed: Base64 syntax, text interpretation, or file restoration. This page is the diagnostic starting point; the other guides explain each layer in depth.
Base64 encoding Korean text and emoji with UTF-8
Compare identical text encoded as different bytes. Select a character encoding from knowledge of the source, not from the language of the website.
Base64 vs Base64URL: differences, padding and decoding examples
Follow a Base64 value through a query string. This guide isolates transport damage from errors in the original value.
Base64 padding explained: what = and == mean
Work out padding from byte groups and check malformed endings. Character encoding and URL transport are covered separately.