How do you encode Unicode text as Base64 correctly?
UTF-8 bytes, the btoa limitation, emoji, and multilingual Base64 conversion.
Unicode text must be converted to UTF-8 bytes before Base64 encoding. JavaScript `btoa()` directly accepts only byte-range characters, so raw use with emoji or many scripts can throw an error or produce the wrong value.
What changed in this update?
The UTF-8 conversion step and browser API sequence were clarified.
What is the correct browser sequence?
`TextEncoder` converts text to UTF-8 bytes. Convert those bytes to a safe binary string before Base64; reverse the process with `TextDecoder` when decoding.
What changes in URL-safe Base64?
The URL-safe variant replaces `+` with `-` and `/` with `_`; padding may be removed. Confirm the receiving system's alphabet and padding rules.
How should conversion be tested?
Round-trip a string containing non-ASCII scripts and emoji. The decoded code points must match the original input.
Frequently asked questions
Why does btoa fail on emoji?
Emoji require multiple UTF-8 bytes and often multiple UTF-16 code units; btoa is not a direct Unicode text API.
Can padding be removed?
Only when the target protocol allows it; decoding may require restoring padding from the length.