What is Base64? A Complete Guide for Beginners
What is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format using a 64-character alphabet. It was originally designed for email attachments via MIME (Multipurpose Internet Mail Extensions) and remains one of the most widely used encoding methods on the web.
When you need to send binary data โ such as an image, a file, or compressed bytes โ over a channel that was built for text, Base64 is the go-to solution. It takes arbitrary bytes and turns them into a safe, portable string of letters, digits, and a couple of special characters.
How Does Base64 Work?
Base64 works by processing binary data in groups of three bytes (24 bits) and converting each group into four Base64 characters. The name "Base64" comes from the fact that the encoding alphabet uses 64 different characters: A-Z, a-z, 0-9, +, and /.
The Encoding Algorithm Step by Step
- Take 3 bytes (24 bits) of input data
- Split them into four 6-bit groups
- Map each 6-bit value (0-63) to the Base64 alphabet table
- Add padding (
=) if the input length is not divisible by 3
Here is a concrete example. The string "Man" encodes as follows:
M(ASCII 77) =01001101a(ASCII 97) =01100001n(ASCII 110) =01101110
Combined: 01001101 01100001 01101110 โ split into 6-bit groups:
010011= 19 โT010110= 22 โW000101= 5 โF101110= 46 โu
So "Man" becomes "TWFu".
What About Padding?
If the input length is not a multiple of 3 bytes, Base64 adds padding characters (=) to make the output a multiple of 4 characters:
- 1 byte of input โ 2 Base64 characters + 2 padding characters (
==) - 2 bytes of input โ 3 Base64 characters + 1 padding character (
=)
For example, the single byte corresponding to "M" encodes to TQ==.
The Base64 Alphabet Table
| Value | Char | Value | Char | Value | Char | Value | Char | |-------|------|-------|------|-------|------|-------|------| | 0 | A | 16 | Q | 32 | g | 48 | w | | 1 | B | 17 | R | 33 | h | 49 | x | | 2 | C | 18 | S | 34 | i | 50 | y | | 3 | D | 19 | T | 35 | j | 51 | z | | 4 | E | 20 | U | 36 | k | 52 | 0 | | 5 | F | 21 | V | 37 | l | 53 | 1 | | 6 | G | 22 | W | 38 | m | 54 | 2 | | 7 | H | 23 | X | 39 | n | 55 | 3 | | 8 | I | 24 | Y | 40 | o | 56 | 4 | | 9 | J | 25 | Z | 41 | p | 57 | 5 | | 10 | K | 26 | a | 42 | q | 58 | 6 | | 11 | L | 27 | b | 43 | r | 59 | 7 | | 12 | M | 28 | c | 44 | s | 60 | 8 | | 13 | N | 29 | d | 45 | t | 61 | 9 | | 14 | O | 30 | e | 46 | u | 62 | + | | 15 | P | 31 | f | 47 | v | 63 | / |
Common Use Cases for Base64 Encoding
Email Attachments (MIME)
Base64 was created for MIME (Multipurpose Internet Mail Extensions). Email protocols like SMTP were designed for 7-bit ASCII text, so binary attachments such as PDFs, images, and ZIP files must be encoded as Base64 before sending over email.
Data URLs in HTML and CSS
Base64 lets you embed images, fonts, and other media directly in HTML or CSS files using data URLs:
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Embedded Image" />
This eliminates an extra HTTP request, which can be beneficial for small assets.
JSON Web Tokens (JWT)
JWTs use the URL-safe variant of Base64 (Base64url) to encode the header, payload, and signature of each token. This is why you see strings like eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 in authentication systems.
Storing Binary in Databases
Many databases are optimized for text storage and may not handle raw bytes gracefully. Encoding binary data as Base64 allows you to store images, serialized objects, or encrypted data in text columns.
API Payloads
When sending binary data through a REST or GraphQL API โ especially in JSON format โ Base64 is the standard choice because JSON cannot natively represent raw bytes.
Does Base64 Increase File Size?
Yes. Base64 encoding increases data size by approximately 33%. For every 3 bytes of input, you get 4 bytes of output. This overhead is the trade-off for making binary data transportable over text-only channels.
Is Base64 Encryption?
No. This is a common misconception. Base64 is an encoding scheme, not encryption. Anyone who receives a Base64 string can decode it back to the original data without needing any key. It provides no security or confidentiality whatsoever. If you need to protect data, use encryption (like AES) and then optionally encode the encrypted bytes with Base64 for transport.
Base64 vs Other Binary-to-Text Encodings
| Encoding | Alphabet Size | Overhead | Best For | |----------|--------------|----------|----------| | Base64 | 64 chars | ~33% | Data transfer and embedding | | Base32 | 32 chars | ~60% | Human-readable, case-insensitive contexts | | Base16 (Hex) | 16 chars | 100% | Debugging, memory dumps, color codes | | Ascii85 | 85 chars | ~25% | Adobe PostScript, PDF streams |
Base64 offers a balanced trade-off between space efficiency and broad compatibility, making it the most popular binary-to-text encoding on the web.
Try Base64 Encoding Online
Ready to encode your own data? Use our free online Base64 encoder and decoder to convert text or files instantly in your browser. No installation or sign-up required.