Base64 Encoding Explained: Binary-to-Text Mathematics, Data URIs, and When to Use It
Learn how Base64 encoding transforms arbitrary binary data into ASCII characters. Discover the 64-character index table, padding rules, and performance tradeoffs.
Dhaval Joshi
Lead Systems Architect at FreeToolkit
Table of Contents
Whenever you inspect an inline SVG background image, an HTTP Basic Authentication header, or an email MIME attachment, you encounter strings of alphanumeric characters ending with occasional = signs. This is Base64—one of the foundational binary-to-text encoding algorithms underpinning modern internet data transfer.
Why Does Base64 Exist?
Early internet routing protocols (such as SMTP for email) were designed strictly for 7-bit ASCII text transmission. When developers attempted to send raw 8-bit binary payloads (like JPEG photos, audio files, or compiled binaries), intermediary mail gateways frequently stripped the 8th high bit or modified control characters (such as line feeds and null bytes), catastrophically corrupting the file.
Base64 was engineered to solve this by mapping raw binary bytes into a standardized subset of 64 printable ASCII characters that traverse any network gateway, proxy, or firewall completely uncorrupted.
The Mathematical Mechanics (24-bit to 4-char)
The Base64 index table uses 64 characters: uppercase A-Z (indices 0–25), lowercase a-z (26–51), digits 0-9 (52–61), plus sign + (62), and slash / (63).
Because 26 = 64, each Base64 character represents exactly 6 bits of information. The algorithm groups 3 standard 8-bit binary bytes (totaling 24 bits) and reorganizes them into 4 separate 6-bit chunks:
Input word: 'Man'
ASCII values: 'M' (77), 'a' (97), 'n' (110)
Binary (24-bit): 01001101 01100001 01101110
Regroup (6-bit): 010011 | 010110 | 000101 | 101110
Decimals: 19 | 22 | 5 | 46
Base64 Output: 'T' | 'W' | 'F' | 'u' ==> "TWFu"
The Mystery of the "=" Padding Character
What happens if the input byte stream is not an exact multiple of 3?
- If 1 byte remains (8 bits): 4 zero bits are appended to create two 6-bit indices, followed by two
==padding characters. - If 2 bytes remain (16 bits): 2 zero bits are appended to form three 6-bit indices, followed by one
=padding character.
The 33% Payload Inflation Overhead
Because 3 bytes of binary data become 4 bytes of ASCII characters, Base64 encoding inherently produces a 33% increase in raw data size (4 / 3 = 1.333). A 1MB photograph encoded in Base64 will weigh approximately 1.33MB over the wire.
Common Modern Use Cases: Data URIs & Auth
- Data URIs: Inlining tiny 1KB placeholder icons directly into CSS or HTML (
<img src="data:image/png;base64,..." />) to eliminate an extra HTTP network request round-trip. - Basic Authentication: Transmitting
username:passwordin the HTTPAuthorization: Basic ...header. - Cryptographic Hashes: Representing SHA-256 and HMAC digest signatures in human-readable strings.
When NOT to Use Base64
Never store large images or video files as Base64 strings inside relational SQL databases or JSON API payloads. The 33% inflation bloats database indexes, exhausts memory caches, and prevents browser HTTP caching. Serve binary assets via CDNs and reference them by URL instead.
Related Free Online Tools
About Dhaval Joshi
Lead Systems Architect at FreeToolkit
Dhaval designs and maintains FreeToolkit’s browser-native processing engines, WebAssembly pipelines, and zero-knowledge privacy architectures. Passionate about web performance, cryptographic systems, and open-source tooling.