Base64 Encoder & Decoder

Encode text to Base64 (RFC 4648) and decode it back, with proper UTF-8 handling, an optional URL-safe alphabet, line wrapping, and a hex codec sharing the same plugin API. Invalid input gets a specific error instead of a silent blank. 100% client-side, nothing leaves your browser.

RFC 4648 UTF-8 safe Plugin codecs 100% client-side

Encode / Decode

Plain textUTF-8 input
Base64 outputread-only
Waiting for input
Input bytes (UTF-8)
–
Output bytes (UTF-8)
–
Size change
–

How Base64 works here

Base64 turns arbitrary bytes into 64 safe ASCII characters. This page walks through the exact encoder, the decoder's validation, and the codec registry that makes it extensible. Every result is computed in your browser.

01

Encode text as UTF-8 bytes, then Base64

Complete

The core idea

Base64 reads input in 3-byte groups (24 bits) and emits four 6-bit indices into the alphabet A–Z a–z 0–9 + /. A short final group is padded with = so the output length is always a multiple of four.

Why UTF-8 first

btoa() only accepts characters in the Latin-1 range. Passing it "世界" throws. The fix is to encode the string to UTF-8 bytes first, convert those bytes to a binary string, and only then call btoa().

  • Step A: TextEncoder turns the string into a Uint8Array of UTF-8 bytes.
  • Step B: each byte becomes one character code 0–255.
  • Step C: btoa() produces the Base64 string.
encode.js
function encodeText(text) {
  const bytes = new TextEncoder().encode(text);
  let binary = '';
  for (const b of bytes) binary += String.fromCharCode(b);
  return btoa(binary);
}

encodeText('Hello');   // "SGVsbG8="
encodeText('世界');    // "5LiW55WM"
Tip

Output is about 33% larger than input: every 3 bytes become 4 characters. The "Size change" stat above shows that overhead for your exact text.

02

Validate on decode and register codecs as plugins

Complete

Strict decoding

Decoding strips whitespace, checks the alphabet with a regex, and rejects a leftover single character (length mod 4 equals 1). After that, the decoder re-checks the bytes as strict UTF-8, so binary data fails with a clear message instead of producing garbage text.

Codec registry

Each codec is an object with label, encode(text, opts) and decode(text, opts). The UI builds its dropdown from the registry, so adding a codec is one registerCodec() call and never touches the layout. The built-in hex codec uses the same API.

registry.js
const CODECS = {};
function registerCodec(id, codec) {
  CODECS[id] = codec;
  codecSelect.add(new Option(codec.label, id));
}

registerCodec('hex', {
  label: 'Hex (UTF-8 bytes)',
  encode(text) { /* ... */ },
  decode(text) { /* throws on bad input */ }
});
Tip

Throw an Error from decode() for bad input. The UI catches it and shows the message, so codecs never need their own error UI.

Codecs
2 built-in
Dependencies
0
Runtime
Browser APIs only
Cost
$0.00

Built with vanilla JS and inline CSS in a single Astro page. Uses TextEncoder, TextDecoder (fatal mode), btoa and atob. Settings persist in localStorage.

Lessons Learned — Build Process

Notes from building the encoder, the strict decoder and the codec registry.

🔤
btoa() only accepts Latin-1

Calling btoa(text) on a string with emoji or CJK characters throws a DOMException. Routing through UTF-8 bytes avoids that. The MDN reference for btoa() documents the Latin-1 restriction.

🧪
Use fatal decoding to catch binary input

A TextDecoder created with fatal: true throws on invalid UTF-8 instead of substituting U+FFFD. Without that flag, decoding a binary file such as a PNG would show a string full of replacement characters with no error.

📐
Follow the RFC for the alphabet, then add options

Standard Base64 and the URL-safe variant differ only in +/ versus -_ and optional padding. The spec is RFC 4648 §4 and §5. The tool keeps the default output standard and makes URL-safe an explicit toggle. For pattern matching on the decoded text, see the Regex Tester.

🔌
A registry keeps the UI generic

Each codec registers an encode and decode pair plus a label. The dropdown, validation and stats code never branch on codec names, so adding a codec (for example Base32) means one registerCodec() call.

Base64 is simple enough to implement in 10 lines, which is why the decoder's error messages matter more than the encoder. Validate early, say exactly what is wrong, and keep the transform itself a pure function.

Related tools
JSON Formatter →

Base64 often wraps JSON payloads in APIs and tokens. Format the decoded result next, or test patterns against it in the Regex Tester.