Whether you’re an experienced C# developer or a beginner, this article will provide you with the information and practical examples you need to efficiently encode and decode data in C# using the Base64 algorithm. This article intends to equip you to work seamlessly with Base64-encoded data within the C# programming language, from comprehending the basics to hands-on demonstrations.
What is Base64?
Base64 is a binary-to-text encoding technique that turns binary data into a printable ASCII string format. It’s widely used to represent data in a way that allows it to be safely transmitted across text-based protocols. This encoding allows binary data, such as images or files, to be efficiently conveyed as text characters. Understanding Base64 is critical in JavaScript for a wide range of data processing tasks and web development apps.
What is C#?
C# (pronounced “See Sharp”) is a general-purpose, high-level programming language that supports multiple paradigms. It encompasses static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. The C# programming language was designed by Anders Hejlsberg from Microsoft in 2000 and was later approved as an international standard by Ecma (ECMA-334) in 2002 and ISO/IEC (ISO/IEC 23270) in 2003. It’s often used in developing software components suitable for deployment in distributed environments.
Does C# Support Base64?
Base64 encoding and decoding is a fundamental technique used in a variety of computer tasks, ranging from processing binary data in a human-readable format to data transmission across text-based protocols. If you’re using C#, you’re in luck because the language has support for Base64 encoding and decoding. Through its standard libraries and methods, C# provides a simple and efficient approach to interact with Base64-encoded data. In the following sections, we’ll look at the native mechanisms that C# provides for dealing with Base64-encoded data, and how you can easily integrate Base64 operations into your programs.
Base64 Encoding in C#
In C#, you can use the Convert.ToBase64String method to encode a string into a Base64 string. This method is part of the System namespace and is available in the System.Runtime.dll assembly.

Example of Base64 Encoding in C#
Here’s an example of how you can use the Convert.ToBase64String
method to encode a string into a base64 string:
string originalString = "B64Encode"; byte[] originalBytes = Encoding.UTF8.GetBytes(originalString); string encodedString = Convert.ToBase64String(originalBytes);
In this example, we first convert the original string into an array of bytes using the Encoding.UTF8.GetBytes
method. Then, we pass this byte array to the Convert.ToBase64String
method to obtain the base64 encoded string.
Example of Converting File to Base64 in C#
Converting a file to a base64 string in C# is a straightforward process. First, you need to read the file into a byte array. This can be done using the File.ReadAllBytes
method, which is part of the System.IO
namespace. Once you have the byte array, you can pass it to the Convert.ToBase64String
method to obtain the base64 encoded string.
string filePath = @"C:\path\to\file"; byte[] fileBytes = File.ReadAllBytes(filePath); string base64String = Convert.ToBase64String(fileBytes);
In this example, we first specify the path of the file we want to convert. Then, we use the File.ReadAllBytes
method to read the file into a byte array. Finally, we pass this byte array to the Convert.ToBase64String
method to obtain the base64 encoded string.
This method may convert any form of file to a base64 string, including text, image, and audio files.
Base64 Decoding in C#
In C#, you can use the Convert.FromBase64String method to decode a base64 string into an array of bytes. This method is part of the System namespace and is available in the mscorlib.dll assembly.
To decode a base64 string into an array of bytes, you can pass the base64 string to the Convert.FromBase64String
method. Once you have the byte array, you may use the proper encoding, such as UTF-8, to convert it to a string.

Example of Base64 Decoding in C#
Here is an example of how to decode a base64 string in C#:
string base64String = "QjY0RW5jb2Rl"; byte[] decodedBytes = Convert.FromBase64String(base64String); string decodedString = Encoding.UTF8.GetString(decodedBytes);
In this example, we first specify the base64 string that we want to decode. Then, we use the Convert.FromBase64String
method to decode the base64 string into an array of bytes. Finally, we convert this byte array into a string using the Encoding.UTF8.GetString
method.
Decoding Base64 to File in C#
In C#, you can decode a Base64 string and write it to a file using the Convert.FromBase64String and File.WriteAllBytes methods.
Here’s an example:
string base64String = "QjY0RW5jb2Rl"; byte[] decodedBytes = Convert.FromBase64String(base64String); string filePath = @"C:\path\to\file"; File.WriteAllBytes(filePath, decodedBytes);
In this example, we first specify the base64 string that we want to decode. Then, we use the Convert.FromBase64String
method to decode the base64 string into an array of bytes. Finally, we specify the path of the file we want to write to and use the File.WriteAllBytes
method to write the decoded bytes to the file.