How to Base64 Encode and Decode Text (and Why Emoji Breaks Most Tools)

2026-12-03来源 dev.toEnglish

Base64 shows up in places you would not expect: embedding a small image in an HTML file, passing a token in an API header, stuffing binary data into a JSON field, or encoding a username and password for basic auth. The encoding itself is simple — every 3 bytes of input become 4 characters of plain ASCII. The part that breaks is the step before it.

The UTF-8 trap that mangles your text

Most quick base64 tools feed your text straight into the browser's built-in btoa(). That function only handles Latin-1 characters. The moment your text has an emoji, an accented letter, or anything non-English — a Hindi word, a Chinese character, a curly quote — btoa() either throws an error or silently produces garbage that decodes into mojibake.

The fix is boring but non-negotiable: convert the text to UTF-8 bytes first, then base64-encode those bytes. On decode, do the reverse — base64 back to bytes, bytes back to UTF-8 text. Any tool that skips the UTF-8 step is only safe for plain English ASCII, and it will not warn you when it is not.

A quick test that exposes a broken tool: encode Hello 👋 नमस्ते and decode the result. If you get the emoji and the Hindi back exactly, the tool handles UTF-8. If you get 👋 or an error, it does not.

When base64 is the right call (and when it is not)

  • Embedding small assets. A tiny icon as a data: URI inside CSS or HTML saves a request. Past a few kilobytes, a separate file is better.
  • Moving binary through text-only channels. JSON, URLs and headers cannot carry raw bytes — base64 is the standard bridge.
  • Basic auth headers. username:password base64-encoded is the format, not a security measure — it is trivially reversible, so never treat it as encryption.
  • Not encryption. Base64 is an encoding, not a lock. Anyone can decode it in one click. Never put a secret in base64 and assume it is hidden.

Three mistakes that corrupt the result

  1. Encoding with the wrong charset. Encoding UTF-16 bytes instead of UTF-8 produces output that looks valid but decodes wrong everywhere else. UTF-8 is the only sane default.
  2. Line breaks inside the output. Some encoders wrap output at 76 characters (the old MIME habit). Paste that into a URL or a header and it breaks. Clean base64 is one unbroken string.
  3. Forgetting the padding. The = at the end is not decoration — it tells the decoder the input length was not a multiple of 3. Strip it and strict decoders reject the string.

The free tool I built

I make BulkTextKit — free text tools that run 100% in your browser, nothing uploaded. The Base64 Encoder / Decoder tool is the one I open for this job: it encodes through UTF-8 first, so emoji and non-English text survive the round trip, and it decodes with the padding handled properly. Paste text, get base64 — or paste base64, get your text back. No account, no limit, and your text never leaves your device.

One line on Pro: BulkTextKit Pro ($19 one-time, no subscription) adds bulk mode — encode or decode up to 50 texts at once — plus saved presets and CSV export.

When has a "simple" encoding step quietly corrupted your data?

原文:https://www.a8q9.cn/en/article/how-to-base64-encode-and-decode-text-and-why-emoji-breaks-most-tools-aa1d85.html

相关文章