Base64 in C#: How to Encode and Decode Strings Without a Headache
\n\nHave 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\nThe 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\nWhat is Base64 and Why Do You Need It?
\n\nBase64 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\nWhere 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
In C#, the System.Convert class handles all of this — your main tool for working with Base64.
How to Encode a String to Base64 in C#
\n\nThe most common scenario: you have a regular string (e.g., "Hello, World!"), and you want to get its Base64 representation.
Here's how it's done:
\n\nusing 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\nImportant 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\nWhy 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\nHow to Decode Base64 Back to a String
\n\nThe reverse process — decoding — looks like a mirror image:
\n\nusing 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\nNote: if you encoded the string in UTF-8, you must decode it in UTF-8 as well. Otherwise, you'll get garbled text.
\n\nWorking with Binary Data (Images, Files)
\n\nBase64 is not limited to strings. You can encode any binary data — for example, an image or a PDF.
\n\nExample: encoding a byte array to Base64
\n\nbyte[] 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\nDecoding back to a file:
\n\nstring base64Image = "..."; // your string\nbyte[] imageBytes = Convert.FromBase64String(base64Image);\nFile.WriteAllBytes("decoded_photo.jpg", imageBytes);\n\nImportant: 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\nCommon Errors and How to Avoid Them
\n\nEven experienced developers sometimes stumble on Base64. Here are the most common pitfalls:
\n\n