bitlvl
EN

Base64 & encodings cheat sheet

Everything about Base64 that is worth looking up, including the one-liners for each language.

Fits on one A4 / Letter page in portrait; use “Save as PDF” in the print dialog to keep a copy.

Alphabet (value → char)

0 A1 B2 C3 D4 E5 F6 G7 H
8 I9 J10 K11 L12 M13 N14 O15 P
16 Q17 R18 S19 T20 U21 V22 W23 X
24 Y25 Z26 a27 b28 c29 d30 e31 f
32 g33 h34 i35 j36 k37 l38 m39 n
40 o41 p42 q43 r44 s45 t46 u47 v
48 w49 x50 y51 z52 053 154 255 3
56 457 558 659 760 861 962 +63 /

URL-safe (RFC 4648 §5): 62 = -, 63 = _, usually no padding. Padding char: =.

How it works

bytes:  01001000 01101001 00100001     "Hi!"
6-bit:  010010 000110 100100 100001
chars:    S      G      k      h       "SGkh"
"Hi"  → 01001000 01101001 (16 bits) → 010010 000110 1001(00) → "SGk" + "="
"H"   → 8 bits → 010010 00(0000)         → "SA"  + "=="
Input length mod 3 = 0no padding
= 12 chars + ==
= 23 chars + =
Output size⌈n / 3⌉ × 4 (≈ +33 %)
Input size from outputlen × 3 / 4 − padding
MIME (RFC 2045)lines of 76 chars, CRLF
PEMlines of 64 chars

Variants

Base64A–Z a–z 0–9 + /; email, JSON, data: URLs
Base64URL- and _ instead of + /; JWT, OAuth, file names
Base32A–Z 2–7; TOTP secrets, case-insensitive, +60 %
Base32hex0–9 A–V; sorts numerically (DNSSEC NSEC3)
Hex (Base16)0–9 a–f; +100 %, trivial to read
Base58no 0 O I l; Bitcoin addresses
Base85 / Ascii85+25 %; PDF, git binary diffs
bcrypt base64./A–Z a–z 0–9, different order; only inside bcrypt hashes

Commands

base64 file; base64 -dGNU coreutils (-w0 for no wrapping)
openssl base64 -AOpenSSL (-A = single line)
echo -n 'text' | base64-n: no trailing newline in the input
base64.b64encode(b), b64decode(s)Python; urlsafe_b64encode for URL-safe
btoa(s), atob(s)JS, Latin-1 strings only; for UTF-8 use TextEncoder + fromCharCode, or Buffer in Node
Buffer.from(s).toString('base64'); Buffer.from(b64, 'base64')Node.js ('base64url' also)
base64.StdEncoding.EncodeToString(b)Go; RawURLEncoding for unpadded URL-safe
base64_encode(), base64_decode()PHP
Base64.strict_encode64(s)Ruby (encode64 wraps lines)
[Convert]::ToBase64String($bytes)PowerShell
SELECT TO_BASE64('x'); encode('x','base64')MySQL; PostgreSQL

Remember

  • Base64 is not encryption; anyone can decode it.
  • Decode errors are usually a missing =, a URL-safe string fed to a strict decoder, or line breaks.
  • Text must be bytes first: pick UTF-8 and say so.
  • A data: URL is data:<mime>;base64,<data>.

Questions

Where do I encode or decode?

The Base64 tool handles text and files in both directions, including URL-safe and unpadded input, and shows the decoded bytes as hex.