How Base64 encoding works
Base64 represents binary data using 64 printable characters: A–Z, a–z, 0–9, + and /. It takes the input three bytes (24 bits) at a time, splits them into four 6-bit groups, and maps each group to one character. That is why the output is always about a third longer than the input.
Text M a n
Bytes 77 97 110
Bits 01001101 01100001 01101110
6-bit 010011 010110 000101 101110
Index 19 22 5 46
Base64 T W F u → "TWFu"
"Hi" has only 2 bytes, so the last group is padded: "SGk="Decoding runs the same steps backwards. The result is bytes, not text: they might be UTF-8 text, a PNG, or a gzip stream. This decoder checks the first bytes (the file signature) to tell which, instead of printing binary as garbled characters.
Base64 variants
| Variant | Differences | Used in |
|---|---|---|
| Standard (RFC 4648) | + and /, padded with = | Data URIs, most APIs, PEM certificates |
| URL-safe (base64url) | - and _ instead of + and /, padding often dropped | JWTs, URL parameters, file names |
| MIME (RFC 2045) | Standard alphabet with a line break every 76 characters | Email attachments |
The decoder accepts all three, so you don't need to know which one you have. For a URL-safe string headed into a query parameter, the URL Encoder & Decoder explains why + and / would otherwise need escaping.
Where you meet Base64
- Data URIs:
data:image/png;base64,iVBORw0…embeds a file directly in HTML or CSS. - HTTP Basic authentication: the
Authorization: Basic …header is justusername:passwordin Base64, which is why it must only be sent over HTTPS. - JSON Web Tokens: the header and payload are base64url-encoded JSON. Paste either part here to read it.
- Kubernetes secrets and CI variables: values are stored Base64-encoded, not encrypted.
- Email: attachments and non-ASCII message bodies travel as MIME Base64.
- APIs: files and images embedded in JSON request or response bodies.
The UTF-8 problem with btoa()
JavaScript's built-in btoa() only accepts characters up to U+00FF. btoa("€") throws an error, and btoa("é") silently produces Latin-1 output (6Q==) that other systems decode as garbage. Convert the text to UTF-8 bytes first:
// Browser
const b64 = btoa(String.fromCharCode(...new TextEncoder().encode("café €")));
const text = new TextDecoder().decode(Uint8Array.from(atob(b64), c => c.charCodeAt(0)));
// Node.js
Buffer.from("café €").toString("base64"); // "Y2Fmw6kg4oKs"
Buffer.from("Y2Fmw6kg4oKs", "base64").toString(); // "café €"
Buffer.from("café €").toString("base64url"); // URL-safe, no paddingBase64 in other languages and the command line
# Python
import base64
base64.b64encode("café".encode()).decode() # 'Y2Fmw6k='
base64.b64decode("Y2Fmw6k=").decode() # 'café'
base64.urlsafe_b64encode(data) # URL-safe variant
# Linux / macOS
base64 file.png > file.txt # encode a file
base64 -d file.txt > file.png # decode (older macOS: -D)
echo -n "text" | base64 # -n stops echo adding a newline
# PowerShell
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("text"))
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("dGV4dA=="))
// Java
Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.UTF_8));
Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
// PHP
base64_encode($data); base64_decode($b64, true); // true = strict modeThe echo -n detail matters: without it the encoded value includes a trailing newline, and a password or API key encoded that way will not match.
Should you inline images as Base64?
Only small ones. A Base64 image is about 33% larger than the file, cannot be cached separately from the HTML or CSS that contains it, and has to be downloaded before the browser can finish parsing that file. For icons and placeholders under a few kilobytes, saving a request can be worth it. For photos, serve a normal image file, and check the size and format with the Blog Image Size Checker.
When decoding fails
- “Not a Base64 character”: the string contains something outside the alphabet, often a quote, a backslash from JSON escaping, or a
%3Dthat should be=(URL-decode it first). - Invalid length: one character is missing or extra, usually because the string was cut off when copied.
- Garbled text: the bytes were not UTF-8. Text encoded from UTF-16 shows a null byte between every character; Latin-1 shows broken accents.
- Binary output: the data is a file, not text. If the type shows as gzip, decompress the downloaded file.
Frequently asked questions
Is Base64 a form of encryption?
No. Base64 is an encoding: anyone can reverse it without a key, exactly as this tool does. It is used to carry binary data through systems that only handle text. Never use it to hide passwords or tokens; a Base64-encoded secret is as exposed as the plain one.
Why do some Base64 strings end with = or ==?
Base64 encodes 3 bytes into 4 characters. When the input length is not a multiple of 3, the last group is padded: one = means the final group held 2 bytes, == means it held 1. Some formats, such as JWTs, drop the padding; this decoder adds it back automatically.
Why is Base64 about a third larger than the original?
Every 3 bytes become 4 characters, so the encoded output is 4/3 of the input size, about 33% larger, plus up to two padding characters. MIME line breaks add roughly 3% more.
How do I convert an image to Base64?
Click Encode a file or drop the image onto the input box. The tool outputs the raw Base64 string, a complete data URI, and ready-made HTML img and CSS background-image snippets you can paste into your code.
How do I decode Base64 to a PDF or image?
Paste the Base64 string, with or without a data: prefix. The tool checks the first bytes to identify the file type, shows a preview for images, opens PDFs in a new tab, and lets you download the file with the correct extension.
Is my data uploaded anywhere?
No. Encoding and decoding run entirely in your browser. Text and files you add never leave your device, so it is safe to decode tokens, certificates or private documents.
Related Tools
- CSV to JSON Converter — Convert CSV to JSON with delimiter detection, correct handling of quoted fields and leading zeros, and array, keyed-object or JSON Lines output.
- JSON to CSV Converter — Convert JSON or JSON Lines to CSV with nested-object flattening, array handling, an Excel-friendly UTF-8 BOM and protection against formula injection.
- Cron Expression Generator — Build and read cron expressions in plain English, see the next 10 run times, and convert between crontab, Spring, Quartz and AWS EventBridge formats.
- CSS Minifier & Beautifier — Minify CSS with only rendering-safe changes, keeping licence comments, strings and calc() intact, or beautify minified CSS into readable, indented code.
- URL Encoder & Decoder — Percent-encode or decode URLs and query strings with component, full-URL, RFC 3986 or form (+) rules.
- Meta Tag Checker — Check a page's title, meta description, robots directives, canonical, viewport and hreflang tags, with a Google result preview and a fix for each issue.