Base64 Encoding Explained
3 min read · Published
Table of contents
Base64 is a way to represent arbitrary bytes using only 64 safe, printable ASCII characters. It exists because many systems (email, JSON, URLs, HTML attributes) were designed for text and break or corrupt raw binary data. You will meet it in data URIs, HTTP Basic authentication, JWTs, email attachments and API payloads.
You can try everything below in the Base64 Encoder/Decoder.
The alphabet
Base64 uses A–Z, a–z, 0–9, + and /, plus = for padding. That is 64 symbols, so each character carries 6 bits of data.
How encoding works
- Take the input bytes and write them as a stream of bits.
- Split the stream into groups of 6 bits.
- Map each 6-bit value (0–63) to a character in the alphabet.
- If the input length is not a multiple of 3 bytes, add
=padding so the output length is a multiple of 4.
Worked example for the text Man:
M a n
01001101 01100001 01101110 (3 bytes = 24 bits)
010011 010110 000101 101110 (four 6-bit groups)
19 22 5 46
T W F u -> "TWFu"
Three bytes become four characters, which is why Base64 output is roughly 33% larger than the input.
Two more examples show padding:
"Ma" -> TWE=
"M" -> TQ==
Base64 in code
// Browser (Latin-1 only!)
btoa('Hello'); // "SGVsbG8="
atob('SGVsbG8='); // "Hello"
// Node.js
Buffer.from('Hello').toString('base64');
Buffer.from('SGVsbG8=', 'base64').toString('utf8');
import base64
base64.b64encode(b"Hello") # b'SGVsbG8='
base64.b64decode("SGVsbG8=") # b'Hello'
The Unicode pitfall
btoa('héllo 👋') throws an error because btoa only accepts characters with code points up to 255. The correct approach is to convert the text to UTF-8 bytes first:
const bytes = new TextEncoder().encode('héllo 👋');
let bin = '';
bytes.forEach((b) => (bin += String.fromCharCode(b)));
const encoded = btoa(bin);
// decode
const out = new TextDecoder().decode(Uint8Array.from(atob(encoded), (c) => c.charCodeAt(0)));
The DeveloperHub tool does this for you, so emoji and non-Latin text round-trip correctly.
URL-safe Base64
+ and / have special meaning in URLs, and = is used in query strings. The URL-safe variant (RFC 4648 section 5) replaces + with - and / with _, and often drops the padding. JWT segments use this form; see JWT Explained for Beginners. If you need to put arbitrary text in a URL, percent-encoding is the other option.
Common uses
- Data URIs:
data:image/png;base64,iVBORw0KGgo…embeds small images in HTML or CSS. - HTTP Basic auth:
Authorization: Basic base64(user:password). This is encoding, not protection; always use HTTPS. - Email (MIME): attachments are Base64-encoded so they survive text-only mail systems.
- JSON APIs: binary data such as files or signatures is sent as a Base64 string, because JSON has no binary type.
Base64 is not encryption
Anyone can decode Base64 instantly. Never use it to hide passwords, tokens or personal data. If you need confidentiality, use real encryption; if you need integrity or fingerprints, use a hash.
When not to use Base64
- For large files over HTTP, send binary directly (multipart upload or raw body). Base64 adds 33% overhead and extra CPU.
- For inline images larger than a few kilobytes, a normal file is cacheable while a data URI is not.
Troubleshooting decode errors
- "Invalid character": the string contains characters outside the alphabet, such as spaces from copy-paste or a URL-safe
-/_in a decoder that expects standard Base64. - Garbled output: the original data was binary (an image or zip), not text.
- Incorrect padding: some systems drop
=; add padding until the length is a multiple of 4.
Related tools
Base64 Encoder/Decoder
Encode text to Base64 or decode it back, with full Unicode support.
URL Encoder
Percent-encode text for safe use in URLs.
JWT Decoder
Decode JWT header, payload and expiry. Never leaves your browser.
FAQ
Why does Base64 end with = signs?
Padding: output length must be a multiple of four characters, so one or two = characters fill the gap when input length is not a multiple of three bytes.
Is Base64 a form of security?
No. It is trivially reversible and offers no confidentiality.
Why can btoa() fail on emoji?
btoa only accepts Latin-1 characters. Encode text as UTF-8 bytes first.
Keep reading
- JWT Explained for Beginners
A beginner-friendly explanation of JSON Web Tokens: structure, claims, signing, expiry, and the security mistakes to avoid.
- REST API Basics
Understand REST APIs: resources, HTTP methods, status codes, JSON payloads, idempotency, pagination, versioning and authentication basics.