Base64 Encode & Decode: how it works and how to build one
Encode text or files to Base64 and decode Base64 back. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Text is encoded as UTF-8, so emoji and non-Latin scripts survive the round trip. Open any file to get its Base64 or a full data: URI for embedding small images in CSS or HTML. URL-safe Base64 (with – and _) is supported too.
When to use it
- Embedding small images in CSS (data URIs)
- Basic auth headers
- Reading tokens and email attachments
How to use it
- Choose Encode or Decode.
- Type text, or open a file to encode it.
- Copy the result, or download decoded binary data.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/base64
How it works
Base64 turns any bytes into 64 safe characters (A–Z, a–z, 0–9, + and /). Every 3 bytes become 4 characters, which is why Base64 is about a third bigger.
The browser’s btoa only accepts Latin-1 text, so the tool first encodes text as UTF-8 bytes with TextEncoder. That way emoji and Hindi text survive. Files are read as bytes with file.arrayBuffer().
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
base64.ts (ts)
export function encodeBytes(bytes: Uint8Array, urlSafe = false) {
let bin = "";
for (let i = 0; i < bytes.length; i += 0x8000) bin += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
const b64 = btoa(bin);
return urlSafe ? b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "") : b64;
}
export function decodeBytes(s: string) {
const clean = s.replace(/\s/g, "").replace(/-/g, "+").replace(/_/g, "/");
const bin = atob(clean + "=".repeat((4 - (clean.length % 4)) % 4));
return Uint8Array.from(bin, (c) => c.charCodeAt(0));
}
export const encodeText = (t: string) => encodeBytes(new TextEncoder().encode(t));
export const decodeText = (s: string) => new TextDecoder("utf-8", { fatal: true }).decode(decodeBytes(s));
Make it production-ready
- Detect when decoded bytes aren’t text and offer a file download instead.
- Build a data: URI using the file’s MIME type.
Questions
Is Base64 encryption?
No. It only changes how bytes are written. Anyone can decode it, so never use it to hide secrets.
How much bigger is Base64?
About 33% bigger than the original bytes.
Free, runs in your browser, nothing is uploaded.