Base64 Encoding and
Decoding Explained
A complete guide to how Base64 works, when to use it, and how to encode or decode Base64 strings instantly in your browser.
B6 What Is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 printable ASCII characters. It was originally designed to allow binary data โ images, audio files, executables โ to travel safely over systems that only handle plain text.
The name comes directly from the alphabet size: 64 characters drawn from AโZ, aโz, 0โ9, and the symbols + and /. Because every output byte is a printable character, Base64-encoded data travels safely through email headers, HTTP requests, JSON payloads, and XML documents without corruption.
Base64 is formally defined in RFC 4648 and is a foundational part of MIME (Multipurpose Internet Mail Extensions), which governs how email attachments and HTML emails are structured.
? How Base64 Encoding Works
Group input bytes in threes. Each group of 3 bytes produces 24 bits of binary data.
Split into four 6-bit groups. Each maps to one Base64 character โ 4 output chars per 3 bytes of input.
Pad with = if needed. If input isn't divisible by 3, padding keeps the 4-character block structure intact.
This means Base64-encoded output is always approximately 33% larger than the original input. A 300-byte file becomes roughly 400 bytes when Base64-encoded.
Worked Example: Encoding "Man"
| Character | ASCII Decimal | Binary (8-bit) |
|---|---|---|
| M | 77 | 01001101 |
| a | 97 | 01100001 |
| n | 110 | 01101110 |
The 24 combined bits are split into four 6-bit groups โ TWFu. That's the Base64 output for "Man".
The Base64 Alphabet
The standard Base64 alphabet maps 64 characters to values 0โ63:
+ and / symbolsThe = character is used exclusively for padding. These characters are a subset of ASCII โ explore the full set on our ASCII Table reference.
Common Use Cases for Base64
Email Attachments (MIME)
SMTP was designed for 7-bit ASCII only. Base64 converts binary attachments โ PDFs, images, executables โ into safe ASCII text that email clients encode and decode automatically.
Data URIs in HTML & CSS
Embed small images directly in HTML or CSS using Base64-encoded data URIs. Eliminates an extra HTTP request โ ideal for icons and loading spinners: data:image/png;base64,โฆ
HTTP Basic Authentication
HTTP Basic Auth encodes username:password in Base64 and places it in the Authorization header. Note: Base64 is encoding, not encryption โ always pair with HTTPS.
JSON Web Tokens (JWT)
JWTs use URL-safe Base64 (Base64url) to encode their header, payload, and signature. This is the standard authentication token format used in REST APIs and SPAs.
Base64 vs Hex Encoding
Both are binary-to-text encoding schemes, but they serve different purposes and have very different size characteristics:
| Property | Base64 | Hex |
|---|---|---|
| Alphabet size | 64 characters | 16 characters (0โ9, AโF) |
| Bits per character | 6 | 4 |
| Size overhead | ~33% larger | ~100% larger |
| Typical use | File transfer, JWTs, data URIs | Crypto hashes, memory dumps, color codes |
For compact encoding use Base64. For inspecting raw bytes use hex. Try our Hex to Text converter and Text to Hex encoder for hexadecimal work.
URL-Safe Base64 (Base64url)
Standard Base64 uses + and /, which have special meanings in URLs. Base64url (RFC 4648 ยง5) replaces them with - and _, and omits padding.
How to Decode Base64
Decoding is the exact reverse: each character is looked up in the alphabet table to recover its 6-bit value, then the groups are reassembled into bytes and converted back to the original data.
Decode Base64 in Common Languages
import base64
base64.b64decode("TWFu")atob("TWFu")base64_decode("TWFu")Base64.getDecoder()
.decode("TWFu")Base64 and Binary: Understanding the Connection
At its core, Base64 is a way of representing binary data as text. Understanding both representations helps when debugging data pipelines, inspecting network traffic, or working with low-level file formats.
Frequently Asked Questions
Is Base64 the same as encryption?
No. Base64 is encoding, not encryption. It converts binary data to text using a fixed, publicly known algorithm โ anyone with a decoder can reverse it instantly. For secure data, use encryption algorithms like AES or RSA in addition to HTTPS transport security.
Why does Base64 output end with = or ==?
The = characters are padding. Because Base64 works in 3-byte input groups, if the input length is not a multiple of 3, the output is padded to maintain the 4-character block structure. One = means one padding byte; two == means two padding bytes.
Can Base64 encode any type of file?
Yes. Base64 operates on raw bytes and is file-format-agnostic. It can encode images (PNG, JPEG, WebP), documents (PDF, DOCX), audio files (MP3, WAV), video files, and any other binary format.
What is the difference between Base64, Base32, and Base58?
All three are binary-to-text encoding schemes with different alphabet sizes. Base32 (32 chars) avoids ambiguous characters like 0 and O. Base58 is used in Bitcoin addresses and removes visually similar characters. Base64 is the most compact and most widely adopted.
How do I embed a Base64 image in HTML?
Encode your image with our Base64 Encoder / Decoder, then embed it using a data URI: <img src="data:image/png;base64,YOUR_STRING">. Useful for small icons where eliminating an HTTP request improves page load time.
More Free Encoding Tools