What is Base64 and how to use it: complete encoding and decoding guide
Learn what Base64 is, how encoding works, when to use it in web development, and how to encode or decode text, images, and files for free.
What is Base64 encoding
Base64 is an encoding scheme that converts binary data (zeros and ones) into a text representation using 64 printable characters. These 64 characters are the letters A-Z, a-z, the digits 0-9, and the symbols + and /, plus the character = for padding.
The core idea is simple: many computer systems can only safely handle plain text. Sending raw binary data (like an image or a compressed file) through email, a URL, or a JSON field can corrupt the data. Base64 solves this by converting binary data into "safe" text that any system can transport without issues.
For example, the 3 binary bytes 01001000 01101111 01101100 (representing "Hol") become the 4 Base64 characters SG9s. The process groups bits into blocks of 6 (instead of 8) and maps them to the 64-character table.
Try encoding right now with the free Base64 encoder from NexTools.
How Base64 works step by step
The Base64 algorithm follows a precise mathematical process that converts every 3 input bytes into 4 output characters:
Step 1: Convert to binary. Each byte of the original text is converted to its 8-bit binary representation. For example, the letter "M" is byte 77 in ASCII, which in binary is 01001101.
Step 2: Group into 6-bit blocks. The bits are reorganized from groups of 8 into groups of 6. If we take "Man" (3 bytes = 24 bits), they split into 4 groups of 6 bits: 010011 | 010110 | 000101 | 101110.
Step 3: Map to the Base64 table. Each 6-bit group (value 0-63) maps to a character in the table: 19=T, 22=W, 5=F, 46=u. Result: "Man" → "TWFu".
Step 4: Padding with "=". If the original bytes are not a multiple of 3, padding is added. With 1 leftover byte, "==" is appended; with 2 leftover bytes, "=" is appended.
Complete numerical example:
| Text | Bytes | Binary | 6-bit groups | Base64 |
|---|---|---|---|---|
| Hi | 72, 105 | 01001000 01101001 | 010010 000110 100100 | SGk= |
| Man | 77, 97, 110 | 01001101 01100001 01101110 | 010011 010110 000101 101110 | TWFu |
| A | 65 | 01000001 | 010000 010000 | QQ== |
The output is always approximately 33% larger than the original (4 characters per 3 bytes). Decode any Base64 text with the NexTools Base64 decoder.
Most common Base64 use cases in 2026
Base64 is present in more parts of modern technology than you might realize:
1. Data URIs in HTML and CSS. You can embed images directly in HTML or CSS code without making a separate HTTP request. For example: <img src="data:image/png;base64,iVBORw0KGgo...">. This eliminates a server request and is ideal for small icons (under 2-3 KB). According to HTTP Archive, 18% of websites in 2025 used at least one data URI in their CSS.
2. Email attachments (MIME). The original SMTP protocol only supports 7-bit ASCII text. To send file attachments (PDFs, images, documents), the MIME standard encodes them in Base64. Every time you receive an email with an attachment, your email client is automatically decoding Base64.
3. JWT tokens (JSON Web Tokens). JWT authentication tokens have 3 parts separated by dots, each encoded in Base64url (a variant that uses - and _ instead of + and /). Millions of APIs use JWT for authentication in 2026.
4. REST APIs and JSON. When you need to send binary data within a JSON payload (which is text), Base64 is the standard solution. Example: sending a user's profile photo as a "avatar": "data:image/jpeg;base64,/9j/4AAQ..." field.
5. Database storage. Some NoSQL databases or text fields in SQL store binary data encoded in Base64. Less efficient than BLOB but more portable across systems.
6. Basic obfuscation. While it is NOT encryption or security, Base64 is used to obfuscate data that shouldn't be human-readable at a glance, such as configuration strings or URL parameters.
Base64 in JavaScript and web development
JavaScript provides native functions for working with Base64 in both the browser and Node.js:
In the browser:
btoa(string)— Encodes a string to Base64. Example:btoa("Hello world")→"SGVsbG8gd29ybGQ="atob(string)— Decodes Base64 to text. Example:atob("SGVsbG8gd29ybGQ=")→"Hello world"
Watch out for Unicode: btoa() fails with Unicode characters (accents, emojis, CJK). For text with special characters you need an intermediate step:
const base64 = btoa(unescape(encodeURIComponent("Text with accents: cafe")));
In Node.js:
Buffer.from(string).toString('base64')— EncodeBuffer.from(base64String, 'base64').toString('utf-8')— Decode
Node.js handles Unicode automatically with Buffer, without the btoa/atob issues.
Practical case: embedding an image. Converting an image to a data URI with JavaScript:
const response = await fetch('/logo.png');
const blob = await response.blob();
const reader = new FileReader();
reader.onloadend = () => console.log(reader.result); // data:image/png;base64,iVBOR...
reader.readAsDataURL(blob);
If you'd rather not write code, use the NexTools Base64 encoder which processes everything in your browser without uploading files to any server.
Base64 vs other encoding methods
It's important not to confuse Base64 with encryption or other encodings. Each one serves a different purpose:
| Method | Purpose | Reversible without key | Secure |
|---|---|---|---|
| Base64 | Represent binary as text | Yes, anyone can decode it | No |
| Hex (Base16) | Represent bytes as hexadecimal | Yes | No |
| URL Encoding | Escape special characters in URLs | Yes | No |
| AES/RSA (encryption) | Protect data with a key | No, you need the key | Yes |
| SHA-256 (hash) | Verify data integrity | No, one-way | Partial |
| UTF-8 | Unicode text encoding | Yes | N/A |
Common mistake: Using Base64 as "security." Base64 does NOT encrypt anything. Anyone can decode it in milliseconds. If you need to protect data, use real encryption (AES-256, for example). If you need to verify integrity, use a hash generator.
When to use each one:
- Need to transport binary via text → Base64
- Need to display bytes in a readable form → Hexadecimal
- Need to put data in a URL → URL Encoding
- Need to protect confidential data → Encryption (AES/RSA)
- Need to verify a file hasn't been modified → Hash (SHA-256)
Base64 variants: standard, URL-safe, and MIME
There isn't just one Base64. Several variants are defined in different RFC standards:
Standard Base64 (RFC 4648): Uses A-Za-z0-9+/ with = padding. This is the most common version, used in MIME, PEM, and most APIs.
Base64url (RFC 4648 §5): Replaces + with - and / with _. Removes or makes optional the = padding. Designed specifically for use in URLs and filenames, since + and / have special meanings in URLs. JWT tokens always use this variant.
MIME Base64 (RFC 2045): Same as standard but inserts line breaks every 76 characters. Used in emails and PEM certificates. If you've ever opened a .pem file, you've seen something like this:
-----BEGIN CERTIFICATE-----
MIICpDCCAYwCCQDU+pQ4pHgSpDANBgkqhkiG9w0BAQsFADAU
MRIwEAYDVQQDDAlsb2NhbGhvc3QwHhcNMjQwMTAxMDAwMDAw
-----END CERTIFICATE-----
Base32: Uses only 32 characters (A-Z2-7). Less efficient (5 bytes → 8 characters vs 3→4 in Base64), but case-insensitive. Used in TOTP (Google Authenticator) and Tor onion addresses.
Practical tip: If working with URLs or JWT, always use Base64url. If encoding data for JSON APIs or emails, use standard Base64. The NexTools encoder supports both variants.
Performance and size: when NOT to use Base64
Base64 has a cost: encoded data is approximately 33% larger than the original. This has significant implications:
Inline images: the 2 KB rule. Embedding images as data URIs is only worthwhile for very small files (icons, sprites). A 100 KB image becomes ~133 KB of Base64 text, and the browser can't cache it as a separate file. The recommended practice in 2026 is:
- Images under 2 KB → data URI (saves one HTTP request)
- Images 2-10 KB → evaluate case by case
- Images over 10 KB → always use a separate file with
<img src="url">
APIs and JSON payloads. If your API needs to send large files, Base64 in JSON is inefficient. Better alternatives:
multipart/form-datafor uploads (no encoding needed)- Presigned URLs (client uploads directly to storage)
- Binary streaming with
Content-Type: application/octet-stream
Database storage. Storing images as Base64 in a TEXT column can work for prototypes, but in production use object storage (S3, Vercel Blob, Cloudinary) and store only the URL.
Concrete overhead numbers:
| Original | Base64 | Overhead |
|---|---|---|
| 1 KB | 1.33 KB | +33% |
| 100 KB | 133 KB | +33 KB |
| 1 MB | 1.33 MB | +330 KB |
| 10 MB | 13.3 MB | +3.3 MB |
How to encode and decode Base64 for free
You have several options for working with Base64 depending on your context:
Option 1: Online tool (the fastest). The NexTools Base64 encoder processes everything in your browser. Paste text or upload a file, and get the result instantly. No data leaves your computer.
Option 2: Command line.
- Linux/Mac:
echo -n "Hello" | base64→SGVsbG8= - Decode:
echo "SGVsbG8=" | base64 --decode→Hello - File:
base64 image.png > image.b64
Option 3: Python.
import base64base64.b64encode(b"Hello").decode()→"SGVsbG8="base64.b64decode("SGVsbG8=").decode()→"Hello"
Option 4: PowerShell (Windows).
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("Hello"))
For most users who just need to quickly encode or decode something, the online tool is the most practical option. If you regularly work with APIs or development, the terminal or your preferred programming language will be more efficient.
You can also convert your data to other formats with the NexTools JSON converter if you need to work with complex data structures.
Try this tool:
Open tool→Frequently asked questions
Is Base64 the same as encryption
No. Base64 is encoding, not encryption. Anyone can decode Base64 without needing a secret key. It's like writing a message in a different alphabet: it changes the representation but doesn't protect the content. To encrypt data, you need algorithms like AES-256 or RSA, which require a key to decrypt.
Why is Base64 encoded data larger than the original
Because Base64 converts every 3 bytes (24 bits) into 4 characters (32 bits), resulting in a 33% increase. Additionally, if the original data is not a multiple of 3 bytes, padding with '=' is added which slightly increases the size. A 1 MB file becomes approximately 1.33 MB in Base64.
What is Base64 used for in emails
The original SMTP protocol only supports 7-bit ASCII text, which prevents sending binary files directly. MIME (Multipurpose Internet Mail Extensions) uses Base64 to encode file attachments as ASCII text that SMTP can transport. Your email client automatically encodes when sending and decodes when receiving.
What is the difference between standard Base64 and Base64url
Standard Base64 uses the characters '+' and '/' which have special meanings in URLs ('+' is a space, '/' is a path separator). Base64url replaces them with '-' and '_' respectively, and makes padding '=' optional. JWT and any data going into a URL should use Base64url to avoid interpretation issues.
Is it safe to put Base64-encoded sensitive data in a URL
No. Base64 encoding provides zero security. Anyone who sees the URL can decode the data instantly. If you need to pass sensitive data via URL, first encrypt it with an algorithm like AES, then encode the encrypted result in Base64url. Or better yet, use reference tokens instead of including the data directly.
Can I encode large images in Base64 for my website
Technically yes, but you shouldn't. A 500 KB image becomes ~667 KB of Base64 text embedded in the HTML, which increases page size, prevents browser caching, and slows loading. The practical rule is to use Base64 data URIs only for images under 2 KB (icons, sprites). For larger images, use separate files with regular img tags.