How to Base64 encode text or a file
- Text: type or paste it into the left box. The Base64 appears on the right as you type.
- Image or file: press Encode a file… or drop the file anywhere on the tool. You get its MIME type, size, an image preview and a data URI.
- Pick options if you need them: URL-safe (-_) for links, file names and tokens, No padding to drop the trailing
=, and Wrap at 76 for email (MIME). - Press Copy, or Download to save the Base64 as a .txt file.
To turn Base64 back into text or a file, use the Base64 decoder, or press Swap ⇄ to decode the output right here.
Base64 encode examples
Text is converted to UTF-8 bytes first, which is what servers, browsers and programming languages expect. These results come from the encoder above:
| Text | UTF-8 bytes | Base64 | URL-safe, no padding |
|---|---|---|---|
| Hello, World! | 13 | SGVsbG8sIFdvcmxkIQ== | SGVsbG8sIFdvcmxkIQ |
| café | 5 | Y2Fmw6k= | Y2Fmw6k |
| 😀 | 4 | 8J+YgA== | 8J-YgA |
| 日本語 | 9 | 5pel5pys6Kqe | 5pel5pys6Kqe |
| ???>>> | 6 | Pz8/Pj4+ | Pz8_Pj4- |
How Base64 encoding works
Base64 (RFC 4648) writes any bytes using 64 characters that survive email, JSON, HTML and URLs: A–Z, a–z, 0–9, + and /. It takes the data 3 bytes (24 bits) at a time and splits them into four 6-bit numbers, each written as one character. The classic example is the word “Man”:
| Text | M | a | n |
|---|---|---|---|
| Bytes | 77 | 97 | 110 |
| Bits | 01001101 | 01100001 | 01101110 |
Regrouped into 6 bits: 010011 010110 000101 101110 = 19, 22, 5, 46 = TWFu. When the input isn't a multiple of 3 bytes, the last group is filled with zero bits and marked with =: “Ma” is TWE= and “M” is TQ==. That is why Base64 output is always about a third larger than the input.
Image to Base64 (data URI)
A data URI puts a whole file inside a URL: data:, the MIME type, ;base64, and the Base64. Browsers treat it like a link to the file, so you can use it in HTML, CSS, Markdown, JSON or email templates without hosting the image. This is a real 1 × 1 pixel PNG (68 bytes):
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" alt="">The encoder can give you the plain data URI, the Base64 on its own, a ready <img> tag or a CSS background-image: url(…). Data URIs can't be cached separately and are a third larger than the file, so keep them for small images:
| File | Original | As Base64 |
|---|---|---|
| 1 KB icon | 1.0 KB | 1.3 KB |
| 20 KB logo | 20.0 KB | 26.7 KB |
| 250 KB photo | 250.0 KB | 333.3 KB |
| 2 MB PDF | 2.00 MB | 2.67 MB |
URL-safe Base64 and padding
Standard Base64 uses + and /, which mean “space” and “path separator” in URLs, and =, which separates query parameters. URL-safe Base64 (base64url, RFC 4648 §5) swaps them for - and _ and usually drops the padding. You can see the difference in the last row of the table above: Pz8/Pj4+ becomes Pz8_Pj4-. JSON Web Tokens are three base64url strings joined by dots; the JWT decoder reads them. If you must put standard Base64 in a URL, percent-encode it with the URL encoder instead.
Base64 encode in JavaScript, Python and the command line
JavaScript: btoa() only accepts characters up to U+00FF. It throws on emoji, and it encodes “é” as the Latin-1 byte (6Q==) instead of UTF-8 (w6k=). Convert to UTF-8 first:
const bytes = new TextEncoder().encode("café 😀");
const b64 = btoa(Array.from(bytes, (b) => String.fromCharCode(b)).join(""));
// Node.js: Buffer.from("café 😀").toString("base64") ("base64url" for URL-safe)Python:
import base64
base64.b64encode("café".encode("utf-8")).decode() # 'Y2Fmw6k='
base64.urlsafe_b64encode(data).rstrip(b"=") # URL-safe, no paddingLinux and macOS: printf '%s' 'Hello, World!' | base64 gives SGVsbG8sIFdvcmxkIQ==. Using echo adds a newline and gives SGVsbG8sIFdvcmxkIQo=. For a file: base64 -w 0 image.png on Linux (no line wrapping) or base64 -i image.png on macOS. Windows PowerShell: [Convert]::ToBase64String([IO.File]::ReadAllBytes("image.png")).
Related developer tools
- Base64 decoder: Base64 back to text, or to an image, PDF or file.
- URL encoder: percent-encode text for query strings and links.
- JWT decoder: read the base64url parts of a JSON Web Token.
Frequently asked questions
How do I Base64 encode a string?
Type or paste the text into the encoder and the Base64 appears as you type. The text is first turned into UTF-8 bytes, and every 3 bytes become 4 Base64 characters. "Hello, World!" encodes to SGVsbG8sIFdvcmxkIQ==. In code: btoa() in JavaScript (after converting to UTF-8), base64.b64encode() in Python, or the base64 command on Linux and macOS.
How do I convert an image to Base64?
Press "Encode a file…" (or drop the image on the tool) and choose a PNG, JPEG, GIF, WebP or SVG. The image is read in your browser and converted to a data URI such as data:image/png;base64,iVBOR… that you can paste into HTML, CSS or JSON. Switch the output to Base64 only, an HTML <img> tag or a CSS url() if you prefer.
Why is the Base64 bigger than the original?
Base64 stores 6 bits per character instead of 8, so 3 bytes become 4 characters: the output is about 33% larger, plus up to 2 padding characters. A 250 KB photo becomes about 333 KB of text. That is why inlining large images as data URIs usually makes pages slower; it pays off only for small icons.
What is URL-safe Base64 (base64url)?
It is the same encoding with two characters swapped: - instead of + and _ instead of /, defined in RFC 4648 section 5. + and / have special meanings in URLs and file names, so base64url is used in links, file names and JSON Web Tokens, usually without = padding. Tick "URL-safe (-_)" and "No padding" to produce it.
Should I keep the = padding?
Keep it unless the system you are sending the data to says otherwise. Standard Base64 (MIME, data URIs, most APIs) expects padding. JWTs and many URL uses drop it, because the length already tells a decoder how many bytes are left. Good decoders, including our Base64 decoder, accept both.
Why does my Base64 differ from another tool?
Usually because the input bytes differ. "echo" adds a newline, so echo 'Hello, World!' | base64 gives SGVsbG8sIFdvcmxkIQo= instead of SGVsbG8sIFdvcmxkIQ==. Some tools encode text as UTF-16 or Latin-1 instead of UTF-8 (é is 6Q== in Latin-1 but w6k= in UTF-8). Line wrapping every 76 characters and URL-safe characters also change the output without changing the data.
What is the "Wrap at 76" option for?
Email (MIME) limits Base64 lines to 76 characters, and PEM files such as certificates use 64. Wrapping only adds line breaks; decoders ignore them. Leave it off for data URIs, JSON, HTTP headers and URLs, where the Base64 must be a single line.
Is Base64 encoding secure?
No, and it is not meant to be. Base64 is a reversible encoding with no key, so anyone can decode it. Use it to move binary data through text-only channels, not to protect passwords, tokens or personal data; those need real encryption.
Are my files uploaded when I encode them?
No. Files are read with the browser's File API and encoded by JavaScript on your device. Nothing is sent to a server or saved, and the file is forgotten when you close the page. Files up to 25 MB can be encoded.