Have you ever wondered how applications send images, files, or other data through APIs? One technique you may come across is Base64 encoding.
Let's understand how it works with a simple example.
What Is Base64?
Base64 is an encoding technique that converts binary data into a string of printable characters. It uses 64 characters: A–Z, a–z, 0–9, +, and /. The = character is used for padding when needed.
Some systems work more easily with text than raw binary data. Base64 helps represent binary data as text so it can be transferred or embedded in text-based formats.
Important: Base64 is encoding, not encryption. Anyone can decode it without a secret key.
How Does Base64 Work?
Base64 processes data in groups of three bytes (24 bits). It splits those 24 bits into four groups of six bits. Each six-bit group maps to a Base64 character.
Let's take the word Man as an example.
Step 1: Convert characters into binary
Each character is represented by one byte using ASCII.
Character | Decimal | 8-bit binary |
M | 77 | 01001101 |
a | 97 | 01100001 |
n | 110 | 01101110 |
Step 2: Combine the binary values
01001101 01100001 01101110
We now have 24 bits.
Step 3: Split into six-bit groups
Base64 divides these 24 bits into four groups of six bits:
010011 010110 000101 101110
Step 4: Map each group to a Base64 character
Each six-bit group represents a decimal value between 0 and 63.
6-bit binary | Decimal | Base64 character |
010011 | 19 | T |
010110 | 22 | W |
000101 | 5 | F |
101110 | 46 | U |
Combining these characters gives us:
Man → TWFu
That's how three bytes become four Base64 characters.
What happens if the input is shorter?
Base64 processes data in three-byte groups. If the final group contains fewer than three bytes, padding is added using =.
For example:
M → TQ==
Ma → TWE=
Base64 in .NET
In a .NET application, you can encode and decode a string using the built-in Convert class.
using System.Text;
string text = "Man";
// Encode
string encoded = Convert.ToBase64String(
Encoding.UTF8.GetBytes(text));
Console.WriteLine(encoded); // TWFu
// Decode
string decoded = Encoding.UTF8.GetString(
Convert.FromBase64String(encoded));
Console.WriteLine(decoded); // Man
Where Is Base64 Used?
Base64 is commonly used for:
Sending binary data through APIs using JSON.
Encoding email attachments.
Embedding small images in HTML or CSS.
Representing the header and payload of a JSON Web Token (JWT).
Final Thoughts
Base64 is a simple and useful way to represent binary data as text. However, it increases the data size by roughly 33% for larger inputs.
Remember: Base64 changes the representation of data, not its security. Never rely on Base64 alone to protect passwords, tokens, or sensitive information.
Happy Coding!

Join the conversation! Your thoughts help the community grow.