What is Base64?
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It's called "Base64" because it uses 64 different characters to represent data: A-Z, a-z, 0-9, +, and /.
Why Do We Need Base64?
Many systems were designed to handle text, not binary data. Email protocols, HTML, CSS, and URLs all have limitations on what characters they can safely transmit. Base64 solves this by converting binary data into a safe text format.
Common Use Cases
- Data URLs: Embedding images directly in HTML/CSS
- Email attachments: MIME encoding for binary files
- API authentication: Basic Auth headers use Base64
- Storing binary in JSON/XML: These formats only support text
How Base64 Works
- Take the binary input
- Split into 6-bit chunks (2^6 = 64 possible values)
- Map each chunk to one of 64 characters
- Pad with "=" if the input isn't divisible by 3 bytes
Size Consideration
Base64 encoded data is approximately 33% larger than the original. This is because 3 bytes of binary become 4 bytes of Base64 text.
// Example: Encoding a string
const encoded = btoa("Hello, World!");
// Result: "SGVsbG8sIFdvcmxkIQ=="
const decoded = atob("SGVsbG8sIFdvcmxkIQ==");
// Result: "Hello, World!"