What Is Base64 Encoding? How It Works and When to Use It
Base64 is one of the most widely used encoding schemes in computing, yet many developers use it without fully understanding how it works. Whether you've seen it in email attachments, API tokens, or data URLs, Base64 is everywhere. This article explains exactly what it is, how the encoding algorithm works, and when you should (and shouldn't) use it.
What Is Base64?
Base64 is a binary-to-text encoding scheme that represents binary data using 64 printable ASCII characters. It converts every 3 bytes of binary data into 4 ASCII characters, making it safe to transmit through text-only channels like email, JSON, XML, and URLs.
The 64 characters used are: A–Z, a–z, 0–9, +, and /. The = character is used for padding when the input length is not divisible by 3.
How Base64 Encoding Works
The encoding process follows these steps:
- Convert input to binary: Each byte of the input is converted to its 8-bit binary representation.
- Split into 6-bit groups: The binary stream is divided into groups of 6 bits each.
- Map to Base64 characters: Each 6-bit group (values 0–63) is mapped to a character from the Base64 alphabet.
- Add padding: If the final group has fewer than 6 bits, zeros are appended, and = padding characters are added to make the output length a multiple of 4.
Example:
Input: "Hi" → 01001000 01101001
6-bit: 010010 000110 100100
Base64: S G k=
So the text "Hi" encodes to "SGk=" in Base64. Try it yourself with our Base64 Encoder/Decoder.
Common Use Cases
| Use Case | Why Base64? |
|---|---|
| Email attachments (MIME) | SMTP only supports 7-bit ASCII; Base64 encodes binary files for safe transmission |
| Data URLs | Embed images directly in HTML/CSS without separate HTTP requests |
| JWT tokens | The header and payload of JWTs are Base64url-encoded JSON |
| API authentication | HTTP Basic Auth encodes credentials as Base64 |
| Binary data in JSON/XML | JSON and XML are text formats that cannot contain raw binary |
Base64 Is Not Encryption
A common misconception is that Base64 provides security. It does not. Base64 is an encoding, not encryption. Anyone can decode a Base64 string instantly — there is no key or secret involved. Never use Base64 to hide passwords, tokens, or sensitive data. For actual security, use proper encryption or hashing algorithms.
Base64 vs Other Encoding Schemes
- Base64 vs URL encoding: URL encoding (percent-encoding) is for making strings safe in URLs, while Base64 is for encoding binary data as text.
- Base64 vs Hex: Hex encoding represents each byte as two hexadecimal characters. It is less space-efficient than Base64 (2x expansion vs 1.33x) but simpler to read.
- Base64 vs UTF-8: UTF-8 is a character encoding for representing Unicode text. Base64 is a binary-to-text encoding that can wrap any binary data, including UTF-8 encoded text.
The Size Overhead
Base64 increases data size by approximately 33%. Every 3 bytes of input becomes 4 bytes of output. For small payloads like tokens or icons, this is negligible. For large files like videos, the overhead becomes significant and Base64 is generally not recommended.
Size Comparison
Original: 1,000 bytes
Base64: 1,336 bytes (~33% larger)
Hex: 2,000 bytes (~100% larger)
Encode or Decode Base64
Instantly encode text to Base64 or decode Base64 strings — free, no sign-up.
Open Base64 Tool →