B6
100% Client-SideAbout
Back to tool
Guide6 min read

How to Encode Text to Base64

A practical guide to encoding text, files, and binary data to Base64 — online, in JavaScript, Node.js, Python, and the terminal.

Want to encode right now?

Paste your text into the free online tool — instant results, no upload.

Encode Now

What does “encode to Base64” mean?

Base64 encoding converts binary data — or any text — into a string of 64 printable ASCII characters (A–Z, a–z, 0–9, +, /). The name “Base64” comes from this 64-character alphabet.

Every 3 bytes of input are grouped together and mapped to 4 Base64 characters, each representing 6 bits. This is why encoded output is always about 33% larger than the original — three bytes (24 bits) become four characters (24 bits spread across 4 × 6-bit groups).

The key purpose: Base64 lets you safely embed binary data inside text-only formats like JSON, XML, HTML, email headers, and environment variables — without worrying about special characters or encoding conflicts.

Encode to Base64 online — step by step

01

Open the Base64 encoder

Go to Base64 Tool and make sure the Text tab is selected. No sign-up or installation required — everything runs in your browser.

02

Switch to Encode mode

Click the Encode button (or toggle) above the input area. The direction indicator will show an arrow pointing from Plain Text → Base64.

03

Type or paste your text

Enter your text in the left panel. The Base64-encoded output appears instantly in the right panel — no button to press. For non-ASCII text (emoji, Arabic, Chinese, etc.), the tool uses TextEncoder internally to handle full UTF-8, unlike the browser's native btoa().

04

Copy the encoded string

Click the Copy button next to the output to copy the Base64 string to your clipboard. To encode images or files (not just text), use the Image or File tabs instead.

Encode Base64 in code

Encode text to Base64 in JavaScript (browser)

The built-in btoa() function encodes strings in the browser, but only handles Latin-1. Use TextEncoder for full UTF-8 support:

// Simple Latin-1 encode (built-in)
const encoded = btoa("Hello, World!");
// → "SGVsbG8sIFdvcmxkIQ=="

// UTF-8 safe encode (handles emoji and non-ASCII)
function encodeBase64(str) {
  const bytes = new TextEncoder().encode(str);
  const binStr = Array.from(bytes, b => String.fromCharCode(b)).join('');
  return btoa(binStr);
}
encodeBase64("Hello 😀"); // → "SGVsbG8g8J+YgA=="

Encode text to Base64 in Node.js

Node.js Buffer handles Base64 encoding natively without any imports:

// Encode a UTF-8 string to Base64
const text = "Hello, World!";
const encoded = Buffer.from(text, "utf8").toString("base64");
console.log(encoded); // → "SGVsbG8sIFdvcmxkIQ=="

// Encode a binary file to Base64
const fs = require("fs");
const fileData = fs.readFileSync("./image.png");
const base64 = fileData.toString("base64");

Encode text to Base64 in Python

Python's standard library includes Base64 encoding in the base64 module:

import base64

# Encode a UTF-8 string to Base64
text = "Hello, World!"
encoded = base64.b64encode(text.encode("utf-8")).decode("utf-8")
print(encoded)  # → SGVsbG8sIFdvcmxkIQ==

# URL-safe Base64 (uses - and _ instead of + and /)
url_safe = base64.urlsafe_b64encode(text.encode("utf-8")).decode("utf-8")

Encode to Base64 in the terminal (macOS/Linux)

Use the built-in base64 command to encode strings or files from the command line:

# Encode a string (note: echo adds a newline — use printf for clean output)
printf 'Hello, World!' | base64
# → SGVsbG8sIFdvcmxkIQ==

# Encode a file to Base64
base64 < image.png > image.b64

Common uses for Base64 encoding

HTTP Basic Authentication

HTTP Basic Auth requires credentials formatted as username:password encoded in Base64 inside the Authorization header.

const credentials = btoa("username:password");
fetch("/api/data", {
  headers: { "Authorization": `Basic ${credentials}` }
});

Embedding images in HTML / CSS

Inline small images directly in your markup using a Base64 data URI, saving an HTTP round-trip.

<!-- HTML -->
<img src="data:image/png;base64,iVBORw0KGgo..." alt="icon" />

/* CSS */
.logo { background-image: url("data:image/svg+xml;base64,PHN2..."); }

Passing binary data through JSON APIs

JSON doesn't support raw binary. Encode binary blobs (images, PDFs, files) to Base64 before including them in JSON payloads.

const payload = {
  filename: "report.pdf",
  content: Buffer.from(pdfBytes).toString("base64"),
  mimeType: "application/pdf"
};
await fetch("/api/upload", {
  method: "POST",
  body: JSON.stringify(payload)
});

Storing secrets in environment variables

Binary keys and certificates can't be stored directly in .env files. Encode them to Base64 first.

# Encode a binary key file
base64 < private-key.pem > key.b64

# In your .env file
PRIVATE_KEY_BASE64="LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0t..."

# Decode at runtime (Node.js)
const key = Buffer.from(process.env.PRIVATE_KEY_BASE64, "base64").toString();

Your data stays private

All encoding happens entirely in your browser using the TextEncoder Web API. No data is ever sent to a server — making this tool safe for encoding sensitive content like API keys, tokens, credentials, and private documents.

Ready to encode your text to Base64?

Free, instant, and 100% private — nothing is ever uploaded to a server.

Open Base64 Encoder