Developer Tools

How to Encode Text and Files to Base64 — Complete Developer Guide

By Habib ur Rehman · December 15, 2025 · Updated June 29, 2026 · 8 min read

The first time I encountered Base64, I was debugging an API integration. The error log showed a long string of seemingly random letters, numbers, and plus signs. I had no idea what I was looking at. A senior developer told me to decode it — and suddenly the error message became readable. That moment taught me two things: Base64 is everywhere in web development, and most people do not understand what it actually does. Here is a complete guide to Base64 encoding — when to use it, how it works, and the mistakes that trip people up.

What Base64 Encoding Actually Does

Base64 takes any kind of data — text, images, PDFs, audio — and converts it into a string of 64 safe, printable ASCII characters. The character set includes uppercase A-Z, lowercase a-z, digits 0-9, plus sign (+), and forward slash (/). The equals sign (=) appears at the end as padding when needed.

The purpose is simple: many systems can only handle plain text. Email protocols like SMTP. JSON APIs. HTML documents. XML files. These systems were never designed to handle raw binary data. Base64 acts as a bridge — it converts binary into text that can travel safely through these text-only channels, then the recipient converts it back.

Original Text: Hello, World!
Base64 Encoded: SGVsbG8sIFdvcmxkIQ==

Where You Encounter Base64 Every Day

Even if you have never intentionally used Base64, you interact with it constantly:

Email Attachments

When you attach a photo or PDF to an email, the email client encodes the attachment as Base64 before sending it through the SMTP protocol. The recipient's email client decodes it back. This happens automatically — you never see the Base64 string, but it is there in the raw email source. The MIME (Multipurpose Internet Mail Extensions) standard, defined in RFC 2045, specifies how Base64 is used for email attachments.

HTTP Basic Authentication

When you access a password-protected API or website, the browser combines your username and password with a colon (username:password) and Base64 encodes the result. This encoded string is sent in the Authorization header. Anyone who intercepts the request can decode it — which is why Basic Auth should always be used over HTTPS, never over plain HTTP.

JSON Web Tokens (JWT)

JWTs are used for authentication in modern web applications. They consist of three parts separated by dots: header.payload.signature. The header and payload are both Base64-encoded JSON. You can take any JWT, copy the middle part, and run it through a Base64 Decoder to see what claims it contains. Developers do this constantly when debugging authentication issues. The JWT standard is defined in RFC 7519.

Data URIs in HTML and CSS

Small images and icons can be embedded directly in HTML or CSS as Base64-encoded data URIs. Instead of linking to an external image file, you include the image data right in the page. This reduces HTTP requests and can speed up page loading for small assets. A data URI looks like this:

<img src="data:image/png;base64,iVBORw0KGgo...">

The Size Trade-Off You Need to Know

Base64 encoding makes data approximately 33% larger. This is not a bug — it is a mathematical consequence of how the encoding works. Every 3 bytes of original data are split into 4 groups of 6 bits. Each 6-bit group maps to one of the 64 characters. Three bytes in, four characters out. Four divided by three equals 1.33 — a 33% increase.

For small amounts of data — authentication tokens, short text strings, tiny icons — this increase is negligible. For large files, it adds up. A 10 MB file becomes about 13.3 MB when Base64 encoded. This is why you should not Base64-encode large files unless you have a specific reason to do so.

Original SizeBase64 SizeIncrease
100 bytes136 bytes36%
1 KB1.37 KB37%
1 MB1.33 MB33%
10 MB13.3 MB33%

Base64 Is NOT Encryption

This is the single most common misunderstanding I encounter. Base64 is encoding — it transforms data into a different format for transmission. It provides zero security. Anyone with a Base64 decoder can reverse it instantly. If you need to protect sensitive data, use actual encryption like AES-256. If you need to safely transmit data through text-based systems, use Base64. They serve completely different purposes. Do not confuse them.

How to Encode Text to Base64

Our Base64 Encoder handles this in your browser — no data is ever uploaded:

  1. Open the Base64 Encoder tool
  2. Type or paste your text into the input box
  3. The tool encodes it instantly — the Base64 output appears in real time as you type
  4. Click Copy to grab the encoded string, or Download to save it as a .txt file

The encoding happens using JavaScript's built-in btoa() function — the same standards-compliant method used by all modern browsers. For decoding Base64 strings back to text, use our Base64 Decoder.

What About Encoding Files Like Images or PDFs?

For text, the process is straightforward. For binary files like images, PDFs, or ZIP archives, you first need to read the file into memory as binary data, then encode that data. The browser's FileReader API handles the reading, and the resulting ArrayBuffer can be Base64 encoded. Our tool handles this automatically when you upload a file — just select your file and the encoded output appears.

Encode Text to Base64 Now

Instant encoding in your browser — free, private, no upload.

Open Base64 Encoder

Questions People Ask About Base64

What is Base64 encoding used for?

Converting binary data into safe text for transmission through email, APIs, JSON, HTML, and other text-based systems. Common in email attachments, JWT tokens, HTTP Basic Auth, and data URIs.

Is Base64 the same as encryption?

No. Base64 is encoding — anyone can reverse it without a key. Encryption requires a secret key and is designed to keep data confidential. Base64 makes data safe for transmission, not safe from prying eyes.

Why does Base64 make my data larger?

It uses only 64 characters to represent all 256 possible byte values. Every 3 bytes become 4 characters — a 33% size increase. This is the trade-off for safe text transmission.

Can I Base64 encode any file?

Yes. Text, images, PDFs, audio, video — any digital file can be Base64 encoded. The output is always a string of printable ASCII characters.

How do I decode Base64 back to text?

Use our Base64 Decoder. Paste the Base64 string and it instantly converts back to the original text. Works the same way — in your browser, nothing uploaded.

Explore Related Developer Tools