If you’ve ever poked around APIs, email attachments, or image data in HTML, chances are you’ve stumbled upon a long, strange-looking string of characters — often ending with =
signs. That’s Base64 Encoding.
In this post, we’ll break down what Base64 Encoding is, how it works, when to use it, and why it’s so widely used in the tech world .
What Is Base64 Encoding?
At its core, Base64 Encoding is a way to represent binary data (like files, images, or raw bytes) as plain text using only readable characters — specifically letters, numbers, +
, /
, and sometimes =
for padding.
This is useful because some systems and protocols (like older email systems or JSON data in APIs) can’t handle binary data directly. Encoding it as text ensures it can safely travel through text-only channels.
Think of it as a translation layer between binary and text.
How Base64 Encoding Works
Here’s the basic idea:
Base64 encoding splits your binary data into chunks of 6 bits each.
Why 6 bits? Because 2 to the power of 6 is 64, which means 64 unique symbols fit perfectly to represent each possible 6-bit sequence.
- Binary Data → Groups of 6 Bits
Computers store everything in binary (0s and 1s). Base64 takes this binary data and processes it in chunks of 6 bits instead of the usual 8 bits (a byte). - Mapping to a Character Set
Each 6-bit chunk maps to one of 64 characters (hence the name “Base64”). The character set includes:
A–Z (26 characters)
a–z (26 characters)
0–9 (10 characters)
+ and / (2 characters)
- Padding with
=
If the total bits don’t divide evenly into 6-bit groups,=
signs are added at the end to keep the encoded string length a multiple of 4.
Example: Encoding a Simple Word
Let’s see what happens when we encode the word Hi
.
Step 1: Convert to ASCII Binary
H → 72 in decimal → 01001000
i → 105 in decimal → 01101001
Step 2: Combine into One Binary Stream
01001000 01101001
Step 3: Split into 6-Bit Groups
010010 000110 1001 (pad with two 0s) → 010010 000110 100100
Step 4: Map to Base64 Table
010010 → S
000110 → G
100100 → k
Step 5: Add Padding
Since we had missing bits at the end, we pad with =
.
Final Base64 Encoding:
SGk=
Base64 Encoding in Code
Here’s a Kotlin example for encoding and decoding:
import java.util.Base64
fun main() {
// Original text
val originalText = "Hello, Base64!"
// Convert the string to bytes (UTF-8 encoding)
val bytes = originalText.toByteArray(Charsets.UTF_8)
// Encode the bytes to Base64 string
val encodedString = Base64.getEncoder().encodeToString(bytes)
// Print the encoded Base64 string
println("Encoded: $encodedString")
}
Here,
- We start with the string
"Hello, Base64!"
stored inoriginalText
. toByteArray(Charsets.UTF_8)
converts the string into a byte array using UTF-8 encoding, which is necessary because Base64 operates on byte data.Base64.getEncoder().encodeToString(bytes)
encodes the byte array into a Base64-encoded string using Java’s built-in Base64 encoder accessible in Kotlin.- Finally, we print the encoded Base64 string.
When you run this code, the output will be:
Encoded: SGVsbG8sIEJhc2U2NCE=
When to Use Base64 Encoding
Base64 Encoding is not for encryption or compression. It’s purely for safe data transport. Here are common use cases:
- Email Attachments (MIME Encoding)
Older email systems can’t handle binary files directly. Base64 makes them safe to send. - Embedding Images in HTML/CSS
Instead of linking to an image file, you can embed it directly as Base64 inside HTML or CSS:
<img src="data:image/png;base64,iVBORw0KGgoAAA...">
- Storing Binary Data in JSON/XML
Many APIs use Base64 to represent file data as plain text. - Authentication (Basic Auth)
In HTTP Basic Authentication, credentials are often Base64 encoded (though this is not secure on its own).
Why Base64 Matters
Base64 Encoding solves a practical problem: moving binary data through systems that only handle text. It’s a universal translator that works across platforms, languages, and protocols.
That said, it comes with trade-offs:
- Larger size: Base64 increases data size by about 33%.
- Not secure: It’s easily reversible, so don’t use it for sensitive data without encryption.
In short, Base64 matters because it keeps data intact during transmission — even if the channel can’t handle raw bytes.
Key Takeaways
- Base64 Encoding turns binary data into text-safe characters.
- It’s about compatibility, not security.
- Use it when data needs to travel through systems that don’t support raw binary.
- Always combine it with encryption if security is a concern.
Conclusion
By understanding Base64 Encoding, you’ll be better equipped to debug API responses, embed resources in code, and handle binary data with confidence.
If you want to try it yourself, grab a Base64 encoder/decoder online and test with some text or an image.
Seeing the transformation in action makes it click instantly.