What is Base64? Encoding Explained Simply
Base64 is one of the most widely used encoding schemes on the web. If you've ever seen a long string of letters and numbers in a URL, email, or CSS file, chances are it was Base64. Here's how it works and why it matters.
Table of Contents
What is Base64?
Base64 is a binary-to-text encoding scheme that represents binary data using 64 printable ASCII characters. It converts images, files, and raw bytes into a safe text string that can travel through text-based systems.
The name comes from the fact that Base64 uses 64 characters. Each character represents exactly 6 bits of information because 2^6 = 64.
Base64 is defined in RFC 4648 and appears in email, data URIs, APIs, JWT tokens, certificates, and many other web formats.
Why Does Base64 Exist?
Many systems were designed to handle text only, not raw binary data. For example:
- Email (SMTP) - originally expected 7-bit ASCII text, so binary attachments need encoding.
- JSON - has no native binary type, so bytes are usually stored as strings.
- URLs - only allow a limited character set safely.
- HTML/CSS - are text formats, so embedded assets need text representation.
Base64 solves this by turning binary data into a safe text representation that these systems can carry without corruption.
How Base64 Encoding Works
The encoding process follows four steps:
- Read the input as a stream of bytes, 8 bits each
- Group bits into chunks of 6 bits
- Map each 6-bit group to a character from the Base64 alphabet
- Add padding with
=if the input length is not divisible by 3
The key idea is that 3 bytes of input become 4 Base64 characters. That is why Base64 increases size by about 33%.
The Base64 Alphabet
The standard Base64 alphabet consists of 64 characters:
| Range | Characters | Values |
|---|---|---|
| Uppercase | A-Z | 0-25 |
| Lowercase | a-z | 26-51 |
| Digits | 0-9 | 52-61 |
| Special | + and / | 62-63 |
| Padding | = | Used for alignment |
There is also a URL-safe variant that replaces + with - and / with _.
Step-by-Step Encoding Example
Let us encode the string Hi! to Base64:
Step 1: Convert to ASCII values
H = 72, i = 105, ! = 33
Step 2: Convert to binary (8 bits each)
01001000 01101001 00100001
Step 3: Regroup into 6-bit chunks
010010 000110 100100 100001
Step 4: Convert to decimal
18 6 36 33
Step 5: Map to Base64 alphabet
S G k h
Result: "Hi!" -> "SGkh"Since Hi! is exactly 3 bytes, it maps cleanly to 4 Base64 characters with no padding.
Understanding Padding (=)
Base64 works on groups of 3 bytes to 4 characters. When the input is not divisible by 3, padding is added:
| Input bytes | Remainder | Padding | Example |
|---|---|---|---|
| Divisible by 3 | 0 | None | "Hi!" -> "SGkh" |
| 1 byte remaining | 1 | == | "H" -> "SA==" |
| 2 bytes remaining | 2 | = | "Hi" -> "SGk=" |
The = padding makes encoded output a multiple of 4 characters, which keeps decoding predictable.
Common Uses of Base64
Base64 is everywhere in modern software development:
- Data URIs - embed small images directly in HTML or CSS.
- Email attachments (MIME) - encode binary files before attaching them to email.
- JWT tokens - use Base64URL encoding for header and payload.
- API authentication - HTTP Basic auth sends credentials as Base64 text.
- Storing binary in JSON/XML - certificates, keys, and binary blobs can be represented as text.
- Source maps - JavaScript source maps use Base64-style VLQ mappings.
Base64 in Code
Most languages have built-in Base64 support. Here are a few examples:
JavaScript
// Encode
const encoded = btoa("Hello, World!");
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="
// Decode
const decoded = atob("SGVsbG8sIFdvcmxkIQ==");
console.log(decoded); // "Hello, World!"Python
import base64
# Encode
encoded = base64.b64encode(b"Hello, World!")
print(encoded) # b'SGVsbG8sIFdvcmxkIQ=='
# Decode
decoded = base64.b64decode(encoded)
print(decoded) # b'Hello, World!'Command Line
# Encode (Linux/macOS)
echo -n "Hello, World!" | base64
# SGVsbG8sIFdvcmxkIQ==
# Decode
echo "SGVsbG8sIFdvcmxkIQ==" | base64 --decode
# Hello, World!Common Misconceptions
There are several things people often get wrong about Base64:
✕ "Base64 is encryption"
Base64 is not encryption. Anyone can decode it instantly, so never use it to hide sensitive data.
✕ "Base64 compresses data"
Base64 actually increases data size by about 33%. It is encoding, not compression.
✕ "Base64 is only for images"
Image data URIs are common, but Base64 can represent any binary data: files, certificates, tokens, and more.
When Not to Use Base64
Base64 is useful, but it is not always the right choice:
- Large files - the 33% size increase adds up quickly.
- Security - use real encryption instead of Base64 for sensitive data.
- Performance-critical paths - encoding and decoding have CPU cost.
- Large images in web pages - separate image files are usually better because browsers can cache them independently.
A good rule of thumb: use Base64 when you must send binary data through a text-only channel. If binary transport is available, prefer that.
Encode & Decode Base64 Instantly
Use our free Base64 Encoder/Decoder tool to encode text to Base64 or decode Base64 strings right in your browser.
Try Base64 Encoder/DecoderReferences
- Josefsson, S. (2006). The Base16, Base32, and Base64 Data Encodings. RFC 4648, IETF. https://datatracker.ietf.org/doc/html/rfc4648
- Freed, N. & Borenstein, N. (1996). Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies. RFC 2045, IETF. https://datatracker.ietf.org/doc/html/rfc2045
- Mozilla Developer Network. btoa() global function. https://developer.mozilla.org/en-US/docs/Web/API/btoa
- Mozilla Developer Network. Data URLs. https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs
- Python Software Foundation. base64 - Base16, Base32, Base64, Base85 Data Encodings. https://docs.python.org/3/library/base64.html