Complete Guide to Base64 Encoding
Understanding Base64 encoding and decoding
What is Base64?
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It converts binary data into a set of 64 printable characters, making it safe to transmit data over text-based protocols.
Why Use Base64?
• Safely transmit binary data over text-based protocols (email, HTTP) • Embed binary data in text formats (JSON, XML, HTML) • Store binary data in databases as text • Encode images for use in CSS or HTML • Handle special characters in URLs
Base64 Character Set
Base64 uses 64 characters: • A-Z (26 characters) • a-z (26 characters) • 0-9 (10 characters) • + and / (2 characters) • = for padding
How Base64 Works
1. Take 3 bytes (24 bits) of binary data 2. Split into 4 groups of 6 bits each 3. Convert each 6-bit group to a Base64 character 4. If input isn't divisible by 3, add padding (=)
Example
Input: "Hi"
Binary: 01001000 01101001
Split: 010010 000110 1001xx
Base64: S G k=
Result: "SGk="Base64 URL Safe
For URLs, the standard + and / characters are replaced: • + becomes - • / becomes _ This is called Base64URL encoding.
Common Use Cases
• Data URIs for embedding images • Email attachments (MIME) • JSON Web Tokens (JWT) • API authentication headers • Storing binary data in cookies