Complete Guide to Base64 Encoding and Decoding
Base64 is one of those things that every developer encounters but few fully understand. It appears in email attachments, data URIs, API tokens, and authentication headers. At its core, Base64 is a way to convert binary data (images, files, bytes) into a safe, text-only format that can be transmitted over systems designed to handle text โ like email or JSON.
What Is Base64 Encoding?
Base64 encoding takes binary data and represents it using 64 different ASCII characters: A-Z, a-z, 0-9, +, and /. This makes the data safe to include anywhere text is expected. The name "Base64" comes from the fact that each character represents one of 64 possible values (2โถ = 64), meaning 6 bits of data per character.
The encoding process works in three-byte chunks. Every 3 bytes (24 bits) of input become 4 Base64 characters (24 รท 6 = 4). If the input is not divisible by 3, padding = characters are added at the end.
Why Developers Use Base64
There are several common use cases where Base64 is the right tool:
- Data URIs โ embed small images directly in HTML or CSS:
data:image/png;base64,... - Email attachments โ MIME uses Base64 to send binary files over SMTP
- API authentication โ Basic Auth headers use Base64-encoded
username:password - JSON-safe binary โ when you need to include binary data in a JSON payload
- Token encoding โ JWT uses a variant of Base64 (Base64URL) for its three segments
Try our Base64 Text Encoder / Decoder to quickly convert any text, or use the Base64 Image Converter to turn images into data URIs.
Base64 and Hashing
Hash outputs like MD5, SHA1, and SHA256 are often displayed as hexadecimal strings, but many systems encode them in Base64 for compact storage. If you need to generate hashes, try our Hash Generator which supports all common algorithms.
Base64 vs Base64URL
Standard Base64 uses + and / as characters 62 and 63, plus = for padding. These characters cause problems in URLs (they are percent-encoded). Base64URL replaces + with - and / with _, and omits padding. This variant is used in JWTs, CSRF tokens, and anywhere Base64 appears in a URL or filename.
The Downsides of Base64
Base64 comes with tradeoffs:
- Size increase โ Base64 is about 33% larger than the original binary data
- CPU cost โ encoding and decoding takes processing time, though negligible for small data
- Caching โ data URIs are not cached separately; every page load re-downloads them
For these reasons, Base64 is best for small assets (under 10 KB) or for data that needs to be text-safe for transport. For large images, serve them as regular files and let the browser cache them.
How to Check Your Base64
If you receive a Base64 string and are unsure whether it decodes correctly, paste it into a Base64 decoder. The tool will validate the string (checking for correct padding and valid characters) and show the decoded output or flag any errors.