Base64 in C#: Encoding and Decoding Strings with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Base64 in C#: How to Encode and Decode Strings Without a Headache

\n\n

Have you ever encountered a situation where you need to transfer binary data over a text protocol, send an image in JSON, or save a hash in a readable format? This is where Base64 comes into play — a good old, reliable format that turns any bytes into safe text.

\n\n

The question "How do I encode and decode a base64 string?" is one of the most popular on Stack Overflow (tens of thousands of views). And for good reason: working with Base64 in C# seems simple, but pitfalls are everywhere. Today we'll cover everything from A to Z: from basic examples to real-world production cases.

\n\n

What is Base64 and Why Do You Need It?

\n\n

Base64 is a way to represent binary data (bytes) as an ASCII string. The alphabet consists of 64 characters: A-Z, a-z, 0-9, plus the '+' and '/' characters. The '=' character is often added at the end of the string for padding.

\n\n

Where it is used:

\n
    \n
  • Data transfer in JSON/XML — binary data cannot be inserted directly, only as a string
  • \n
  • Authentication — Basic Auth uses Base64 to encode the username and password
  • \n
  • Storing images in a database — small images are stored directly in the database as Base64
  • \n
  • Email attachments — the MIME format requires Base64 for binary files
  • \n
\n\n

In C#, the System.Convert class handles all of this — your main tool for working with Base64.

\n\n

How to Encode a String to Base64 in C#

\n\n

The most common scenario: you have a regular string (e.g., "Hello, World!"), and you want to get its Base64 representation.

\n\n

Here's how it's done:

\n\n
using System;\nusing System.Text;\n\nstring original = "Hello, World!";\nbyte[] bytes = Encoding.UTF8.GetBytes(original);\nstring base64 = Convert.ToBase64String(bytes);\n\nConsole.WriteLine(base64); // Output: SGVsbG8sIFdvcmxkIQ==
\n\n

Important point: Base64 works with bytes, not strings. So first we convert the string to a byte array (using UTF-8 encoding), and then encode those bytes.

\n\n

Why UTF-8 and not, say, ASCII? Because UTF-8 supports all Unicode characters, including Cyrillic, hieroglyphs, and emojis. If you use ASCII for Russian strings, data will be lost.

\n\n

How to Decode Base64 Back to a String

\n\n

The reverse process — decoding — looks like a mirror image:

\n\n
using System;\nusing System.Text;\n\nstring base64 = "SGVsbG8sIFdvcmxkIQ==";\nbyte[] bytes = Convert.FromBase64String(base64);\nstring decoded = Encoding.UTF8.GetString(bytes);\n\nConsole.WriteLine(decoded); // Output: Hello, World!
\n\n

Note: if you encoded the string in UTF-8, you must decode it in UTF-8 as well. Otherwise, you'll get garbled text.

\n\n

Working with Binary Data (Images, Files)

\n\n

Base64 is not limited to strings. You can encode any binary data — for example, an image or a PDF.

\n\n

Example: encoding a byte array to Base64

\n\n
byte[] imageBytes = File.ReadAllBytes("photo.jpg");\nstring base64Image = Convert.ToBase64String(imageBytes);\n\n// Now you can save the string in JSON or send it via API\nConsole.WriteLine($"Base64 string length: {base64Image.Length} characters");
\n\n

Decoding back to a file:

\n\n
string base64Image = "..."; // your string\nbyte[] imageBytes = Convert.FromBase64String(base64Image);\nFile.WriteAllBytes("decoded_photo.jpg", imageBytes);
\n\n

Important: Base64 increases data size by approximately 33%. If you have a 1 MB image, after encoding it will weigh ~1.33 MB as a string.

\n\n

Common Errors and How to Avoid Them

\n\n

Even experienced developers sometimes stumble on Base64. Here are the most common pitfalls:

\n\n

Blogs

Book Recommendations