Yueseryueser.com

← All guides

Base64 encode and decode: UTF-8 text without mojibake

Paste text and press Encode to get Base64, or paste Base64 and press Decode to get the text back. The tool converts to UTF-8 bytes before encoding, so non-ASCII text does not throw the way a direct btoa() call does.

Open the tool:Base64

When Base64 is the right tool

You need to put non-ASCII text into a field that only accepts ASCII: an HTTP header, a config value, or a protocol field limited to printable characters. Base64 maps arbitrary bytes onto a safe 64-character alphabet.

You receive a Base64 string and want to know whether it decodes to readable UTF-8 or is just binary noise. A result full of � usually means the underlying bytes are not UTF-8.

You are inspecting the payload of a JWT, a Basic Auth header, or a data URL. Those payloads are Base64 or Base64URL; decode first, then read. Note that JWT uses Base64URL, whose alphabet and padding rules differ slightly.

How to get it right

Do not hand a JavaScript string straight to btoa(). btoa() accepts a binary string where every character is in the 0–255 range and throws InvalidCharacterError on anything else. The correct order is TextEncoder to UTF-8 bytes, then btoa() over those bytes; decoding reverses it with atob() and TextDecoder('utf-8').

The output grows by about a third. Base64 turns every 3 bytes into 4 ASCII characters, so the length becomes 4/3 of the input, with = padding when the byte count is not a multiple of 3. In UTF-8, one CJK character is usually 3 bytes and becomes about 4 characters, while one emoji is usually 4 bytes and becomes about 8 characters including padding. That is encoding overhead, not compression.

Whitespace is ignored when decoding, but characters outside the Base64 alphabet cause an error. If the string picked up stray symbols, clean them out first.

Decoding Base64 that is not UTF-8 produces � (U+FFFD). The tool is not broken; the original bytes simply are not valid UTF-8. Images and archives should never be decoded as text in the first place.

For URLs, switch to Base64URL: + becomes -, / becomes _, and trailing = is dropped. In a query string a literal + can be read as a space under form rules, and / can collide with path separators.

Finally, Base64 is not encryption. There is no key, anyone can decode it, and using it to hide a password or token only changes the wrapper. Use AES-GCM when you need confidentiality, and a signature or hash when you need integrity.

Alternatives and limits

Do not convert binary files with a text tool. Use the base64 command line, base64 -i on macOS, certutil -encode on Windows, or read an ArrayBuffer in code, so the bytes survive exactly.

For confidentiality use WebCrypto AES-GCM; for authentication use HMAC or a public-key signature. Base64 only makes bytes safe to transport as characters and provides no security property.

This tool handles text only: input is interpreted as UTF-8 when encoding and restored as UTF-8 when decoding, so binary bytes cannot survive, and there is no streaming, so very large inputs consume a lot of memory.

FAQ

Why does Base64 make the text bigger?

Because it turns every 3 bytes into 4 ASCII characters, so the size becomes about 4/3 of the original — roughly a third larger. When the byte count is not a multiple of 3, the end is padded with one or two = characters. In UTF-8 a Chinese character is typically 3 bytes and encodes to about 4 Base64 characters; an emoji is typically 4 bytes and encodes to about 8 characters including padding.

Is Base64 encryption?

No. Base64 is a reversible encoding with no key: anyone who has the string can decode it. It hides nothing and authenticates nothing. Confidentiality requires real encryption such as AES-GCM, and integrity requires a signature or a hash. Base64 is only about getting bytes through text-only channels without corruption.

Can I encode an image or a PDF with this?

No. The tool only accepts text: when encoding, TextEncoder treats the input as UTF-8 text, so the bytes of a binary file are not preserved; when decoding, bytes that are not valid UTF-8 become � (U+FFFD) and the file is corrupted. Use the command line (base64 on macOS and Linux, certutil on Windows) or a tool that reads binary data.

Related tool:Base64