What Base64 actually does

Base64 maps every byte to one of 64 characters (A–Z, a–z, 0–9, +, /) so binary data can travel through text-only channels such as JSON, email and URLs. It is an encoding, not encryption: anyone can decode it.

Encoding expands the data by roughly 33% because three bytes become four characters. Padding with = fills the final group when the input length is not a multiple of three.

text
input:    Hello
encoded:  SGVsbG8=
bytes:    5 -> 8 characters

Unicode and UTF-8

The tricky part of Base64 is deciding which bytes to encode. JavaScript's btoa operates on Latin-1 code units, so btoa('中文') throws. The correct approach is to encode the string to UTF-8 first, then Base64 the bytes. This encoder does exactly that.

If another tool gives you a different Base64 for the same Chinese text, compare the UTF-8 bytes. Different encodings (UTF-16, GBK) produce different Base64 even for identical visible text.

URL-safe variant

Standard Base64 uses + and /, which are meaningful in URLs. The URL-safe alphabet replaces them with - and _ and often drops padding. Use it for JWT segments, query parameters and filenames.