How Base64 Encoding Works (With Examples)
Base64 converts bytes into 64 text characters so binary data can travel in text. It reads 3 bytes (24 bits) at a time and writes them as four 6-bit groups, each mapped to A to Z, a to z, 0 to 9, + or /. "Hello" becomes SGVsbG8=.
Step by step: encode "Hi"
- Convert to bytes: H = 72, i = 105.
- In binary: 01001000 01101001.
- Split into 6-bit groups: 010010 000110 1001 (pad with zeros: 100100).
- Values: 18, 6, 36 → letters S, G, k.
- Add = padding to reach 4 characters: SGk=.
The 64 characters
Values 0–25 are A–Z, 26–51 are a–z, 52–61 are 0–9, 62 is + and 63 is /. The = sign pads the end.
Why 33% bigger?
Three bytes (24 bits) become four characters, so output is 4/3 the size of the input. 300 bytes become 400 characters.
| Text | Base64 |
|---|---|
| A | QQ== |
| Hi | SGk= |
| Hello | SGVsbG8= |
| CalculatorHub | Q2FsY3VsYXRvckh1Yg== |
URL-safe Base64
The + and / characters have special meanings in web addresses, so a URL-safe form uses − and _ instead and often drops the = padding.
Not encryption
Base64 is a reversible encoding. Anyone can decode it, so it does not protect secrets. Passwords and tokens that are "Base64 encoded" are not hidden.
Where you will see it
- Email attachments, data URLs, JSON web tokens and HTTP basic authentication headers.
Convert text with the Base64 encoder and decoder, and see how URL encoding works.
Key terms
- Byte: Eight bits.
- Padding: The = characters that complete the last group.
- Encoding: Changing how data is written.
- Binary: Data stored as 0s and 1s.
- MIME: The email standard that uses Base64 for attachments.
Practice questions
Try each one before you read the answer.
- How many characters does Base64 give for 6 bytes?
Answer: 8 characters. - How many for 4 bytes?
Answer: 8 characters including padding (4 bytes need 2 groups). - Is Base64 reversible without a key?
Answer: Yes.
Common mistakes with Base64
- Assuming Base64 hides data. It does not, so never store passwords or secrets this way.
- Adding line breaks or spaces inside the encoded text, which some decoders reject.
- Forgetting that text must be turned into bytes (UTF-8) before encoding, so accents and emoji need a decoder that expects UTF-8.
- Using the standard alphabet in a web address. Use the URL-safe form with - and _ instead.
Size and speed
Because every 3 bytes become 4 characters, a 1 MB image becomes about 1.33 MB as Base64. That overhead is why large files are usually sent as binary rather than Base64 text. For small icons in a web page, a Base64 data URL can save a request, but it cannot be cached separately, so use it sparingly.
Decoding by hand
Look up each character's value (A is 0, B is 1, and so on), write each as 6 bits, join all the bits and split them into groups of 8 to get the original bytes. Ignore any = padding at the end.
Base64 in everyday tools
You meet Base64 more often than you think. A photo attached to an email is turned into Base64 text before it is sent. A login using basic authentication sends 'username:password' as Base64. A JSON web token is three Base64URL parts separated by dots. In every case the idea is the same: carry bytes through a channel designed for text, then decode them at the other end.
Frequently asked questions
Why does Base64 have = at the end?
Padding, so the length is a multiple of 4 characters.
Can I decode Base64 without a key?
Yes. Base64 has no key, which is why it is not secure.
Is Base64 compression?
No. It makes data about 33% larger.