Base64 Encoding: What It Is and When to Use It
By ToolPix Team
What Is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 ASCII characters. It was designed to allow binary data — images, files, cryptographic hashes — to be safely transmitted through channels that only support text, such as email (SMTP), JSON, XML, HTML, and URLs.
The name "Base64" refers to the 64-character alphabet used: A-Z (26), a-z (26), 0-9 (10), + and / (2), with = used for padding. This set was chosen because these characters are universally safe in text-based protocols without requiring escaping.
How Base64 Encoding Works
The encoding process converts every 3 bytes (24 bits) of binary input into 4 Base64 characters (6 bits each):
- Take 3 bytes of input (24 bits total).
- Split into 4 groups of 6 bits.
- Map each 6-bit group to a character in the Base64 alphabet (A-Z = 0-25, a-z = 26-51, 0-9 = 52-61, + = 62, / = 63).
- If the input is not a multiple of 3 bytes, pad with one or two = characters so the output length is always a multiple of 4.
Example
The text "Hi!" in ASCII is bytes [72, 105, 33].
- In binary: 01001000 01101001 00100001
- Regrouped into 6-bit units: 010010 000110 100100 100001
- Decimal values: 18, 6, 36, 33
- Base64 characters: S, G, k, h
- Result: "SGkh"
You can verify this with our Base64 Encoder/Decoder tool.
The 33% Size Overhead
Because Base64 encodes 3 bytes into 4 characters, the output is always approximately 33% larger than the input. This is an inherent trade-off of the encoding — you gain text compatibility at the cost of increased size.
For a 100 KB image, the Base64-encoded version will be approximately 133 KB. For a 1 MB file, approximately 1.33 MB. This overhead is important to consider when deciding whether Base64 is the right approach for your use case.
Common Use Cases
Data URIs (Inline Images in HTML/CSS)
Data URIs embed file content directly in HTML or CSS using Base64:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEU..." alt="icon">
This eliminates the HTTP request for the image — the browser decodes it directly from the HTML. This is most useful for:
- Small icons and decorative images (under 2-3 KB). The saved HTTP request outweighs the 33% size increase.
- Email templates where external image references may be blocked by email clients.
- Single-file HTML documents that need to be self-contained.
Our Image to Base64 tool converts any image to a ready-to-use data URI. And our Base64 to Image tool does the reverse — converting a Base64 string back to a downloadable image file.
API Payloads (JSON)
JSON does not support binary data natively. When APIs need to transmit binary content (images, documents, cryptographic keys) within JSON payloads, Base64 encoding is the standard approach:
{ "avatar": "data:image/jpeg;base64,/9j/4AAQSkZJRg...", "filename": "photo.jpg" }
This is common in REST APIs where multipart form data is not desired or supported. GraphQL APIs, which use JSON exclusively, frequently use Base64 for file uploads.
Email Attachments (MIME)
Email was originally designed for 7-bit ASCII text only. Base64 encoding (specified in MIME — Multipurpose Internet Mail Extensions) allows binary attachments to be transmitted through email infrastructure. Every email attachment you have ever sent was Base64-encoded behind the scenes.
Cryptographic Data
Cryptographic hashes, encryption keys, digital signatures, and certificates are binary data that frequently needs to be represented as text. Base64 is the standard encoding for:
- PEM certificates and keys
- JSON Web Tokens (JWT) — the header and payload are Base64url-encoded
- HTTP Basic Authentication — credentials are Base64-encoded
You can generate hashes and then encode them with our Hash Generator and Base64 Encoder/Decoder.
Storing Binary in Text Databases
Some databases, configuration files, and data stores only support text. Base64 encoding allows binary content to be stored in these text-only contexts. This is also common in environment variables and configuration management systems.
When NOT to Use Base64
Base64 is not always the right choice:
- Large files: The 33% overhead becomes significant with large files. A 10 MB image becomes ~13.3 MB in Base64. For large files, use binary transfer (multipart upload, binary protocols).
- Web images over 2-3 KB: For images larger than a few KB, a separate HTTP request with proper caching is more efficient than inline Base64. The Base64 data cannot be cached independently from the HTML/CSS.
- When binary transfer is available: If the channel supports binary (HTTP file upload, WebSocket binary frames, gRPC), use binary — it is more efficient.
- Security by obscurity: Base64 is not encryption. It is trivially reversible and provides zero security. Never use Base64 to "hide" sensitive data.
Base64 Variants
- Standard Base64 (RFC 4648): Uses +, /, and = padding. The most common variant.
- Base64url: Replaces + with - and / with _, omits padding. URL-safe — used in JWTs, URLs, and filenames.
- MIME Base64: Same as standard but adds line breaks every 76 characters. Used in email.
Encode and Decode with ToolPix
Our Base64 Encoder/Decoder handles both encoding and decoding instantly in your browser. Paste text to encode it, or paste a Base64 string to decode it. No server processing, no data collection — everything runs locally.
For image-specific Base64 operations, use our Image to Base64 and Base64 to Image tools.