Base64 Encoding & Decoding: Binary-to-Text Representation (RFC 4648)
Base64 is a binary-to-text encoding scheme that represents binary assets, cryptographic keys, authentication tokens, and inline data URIs using a set of 64 ASCII-printable characters.
Encoding Mechanics & Padding Rules
Base64 works by taking groups of three 8-bit bytes (24 bits total) and re-dividing them into four 6-bit chunks (2^6 = 64 possible values):
- Character Alphabet: Consists of uppercase
A–Z, lowercasea–z, digits0–9, and symbols+and/. - Padding Character (=): When input data length is not divisible by 3, one or two trailing equal signs (
=) are appended to preserve 4-byte block boundaries. - Data Expansion: Because 3 bytes become 4 bytes, Base64 encoding increases data volume by approximately 33% relative to raw binary.
Standard Use Cases & Data URIs
Base64 is foundational across modern software engineering workflows:
- Data URIs: Inlining small SVG or PNG icons directly into CSS or HTML (
data:image/png;base64,...) eliminates round-trip HTTP requests. - HTTP Basic Authentication: Encodes
username:passwordcombinations inside theAuthorization: Basic ...request header. - JWT Tokens: JSON Web Tokens use Base64URL encoding (substituting
-and_for+and/) to safely convey payload claims.
Pro Tip: Practical Application
For multi-byte Unicode strings (such as emojis or non-Latin alphabets), use UTF-8 byte encoding before Base64 transformation to avoid standard btoa character range exceptions.