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.
Encode / Decode
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.
Encode text as UTF-8 bytes, then Base64
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:
TextEncoderturns the string into aUint8Arrayof UTF-8 bytes. - Step B: each byte becomes one character code 0–255.
- Step C:
btoa()produces the Base64 string.
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" 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.
Validate on decode and register codecs as plugins
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.
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 */ }
}); Throw an Error from decode() for bad input. The UI catches it and shows the message, so codecs never need their own error UI.
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.
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.
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.
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.