Developer Tools · 5 min read · Updated 2026-11-20

UTF-8 Safe Base64 & Base64URL Encoding Without Garbled Accents or Crashes

Calling window.btoa() directly on non-Latin1 characters throws InvalidCharacterError because btoa accepts only 0–255 byte values.

1. Modern TextEncoder + Chunked Base64 Conversion

Instead of deprecated unescape(encodeURIComponent(str)) hacks, modern browsers convert strings to UTF-8 byte arrays via TextEncoder, process chunks safely to avoid call-stack limits on large buffers, and validate decoded bytes strictly with TextDecoder("utf-8", { fatal: true }).

UTF-8 String -> TextEncoder -> Uint8Array (32 KB chunks) -> btoa()

2. Standard Base64 vs. URL-Safe Base64URL

Base64URL replaces + and / with - and _ and typically strips trailing = padding so tokens pass cleanly inside URL query strings and JWT headers/payloads.

Try the Interactive Browser Tool

Chunked UTF-8 safe Base64 and Base64URL encoding/decoding for Vietnamese diacritics, emoji, and JSON.

Related Guides & Utilities