Base64 is a binary-to-text encoding scheme that represents arbitrary byte data using only 64 printable ASCII characters (A–Z, a–z, 0–9, +, /), so binary content can travel safely through systems — email, JSON, URLs, HTML — that only reliably handle text. It's defined in RFC 4648.
Base64 groups input bytes into chunks of 3 (24 bits) and re-splits each chunk into four 6-bit groups, mapping each 6-bit value (0–63) to one character in the Base64 alphabet. Because 6 bits can only represent 64 distinct values, three arbitrary bytes always expand to exactly four encoded characters — a fixed 4:3 size overhead of roughly 33%. When the input length isn't a multiple of 3, the last group is padded with one or two = characters so the output length always stays a multiple of 4.
A URL-safe variant, Base64URL (RFC 4648 §5), swaps +→- and /→_ so the output can appear in a URL or filename without percent-encoding, and conventionally omits the = padding since the decoder can infer it from length.
Example:
Input (bytes): "hi!" (3 bytes → no padding)
Base64: "aGkh"
Input (bytes): "hi" (2 bytes → 1 padding char)
Base64: "aGk="
+ and / characters need percent-encoding inside a URL — use Base64URL instead to avoid double-encoding bugs.+// string to a URL-safe-only decoder) fails silently or throws, depending on the library.