What is Base64 Encoding: How It Works, Formulas & Use Cases
Everything you need to know about Base64 encoding and decoding. Understand radix-64 math, binary-to-ASCII conversion, padding rules, and performance impacts.
Try the Base64 Encoder / Decoder
Calculate your numbers instantly in your browser. Fast, free, no signup.
Base64 is a binary-to-text encoding algorithm that translates arbitrary binary sequences or non-ASCII characters into an ASCII string format. It represents binary data using a set of 64 printable characters, ensuring data remains intact without modification during transit across network protocols designed strictly for plain text.
From embedding inline images in HTML/CSS to formatting authentication credentials in HTTP headers, Base64 is a ubiquitous building block of modern web infrastructure.
Why Do We Need Base64?
Historically, legacy communication channels like email (SMTP) and early network gateways were designed to handle only 7-bit US-ASCII text. When raw 8-bit binary data (such as compiled executables, images, or compressed archives) traversed these networks, intermediary routers frequently misinterpreted or stripped byte sequences like control codes or line endings (\r, \n).
Base64 solves this by mapping every possible combination of bits strictly to characters universally supported across all systems:
- Uppercase alphabet:
A-Z(indices 0–25) - Lowercase alphabet:
a-z(indices 26–51) - Digits:
0-9(indices 52–61) - Punctuation symbols:
+and/(indices 62–63)
How Base64 Mathematical Encoding Works
The algorithm groups binary input into chunks of 24 bits (3 bytes of 8 bits each) and re-slices them into four 6-bit units:
Input (3 Bytes): [ Byte 1 (8b) ] [ Byte 2 (8b) ] [ Byte 3 (8b) ]
Total Bits: |------------------ 24 bits -------------------|
Regrouping (4x 6b): [ 6 bits ] [ 6 bits ] [ 6 bits ] [ 6 bits ]
Base64 Index: (0 to 63) (0 to 63) (0 to 63) (0 to 63)
Mapped ASCII: Char 1 Char 2 Char 4 Char 4
The 33% Storage Overhead Tradeoff
Because 3 bytes (24 bits) of raw data produce 4 bytes (32 bits) of encoded ASCII text, Base64 encoding always increases data size by approximately 33%:
Encoded Size ≈ ceil(n / 3) × 4 bytes
What Is the = Padding Character?
What happens if the input byte sequence is not evenly divisible by 3?
- If 1 byte remains (8 bits):
The byte is followed by 4 zero bits to form two 6-bit values. The remaining two character slots in the 4-character block are filled with
=padding characters (==). - If 2 bytes remain (16 bits):
They are followed by 2 zero bits to form three 6-bit values. The final slot is padded with a single
=character.
Common Web Developer Use Cases
- HTTP Basic Authentication:
Credentials sent in the
Authorizationheader take the formatBasic base64(username:password). - Data URIs in Web Performance:
Small SVG icons, audio clips, or fonts can be inlined directly inside CSS or HTML using
data:image/png;base64,...to eliminate extra HTTP round trips. - Cryptographic Tokens & JWTs:
JSON Web Tokens encode header, payload, and signature data using a URL-safe variation of Base64 (
Base64URL, which substitutes-and_for+and/).
Test your strings or decode payloads directly in our client-isolated Base64 Encoder & Decoder.
ToolsHub Editorial Team
Verified AuthorTechnical Architect & Lead Developer • ToolsHub Pro
Abdul Nabi is a senior software engineer and technical architect specializing in client-side Web APIs, high-performance browser computing, cryptographic validation, and privacy-first digital utility platforms. All guides on ToolsHub Pro are peer-reviewed for mathematical rigor and developer accuracy.