Sai A Sai A
Updated date May 02, 2023
In this blog, we will explore three methods for converting Base64 strings to plain text strings in C# using the Convert.FromBase64String method and either the Encoding.ASCII.GetString, Encoding.UTF8.GetString, or StreamReader.ReadToEnd methods.

Introduction:

Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. Base64 is often used in computer science applications to transmit data over media that are not 8-bit clean, such as email systems or HTML forms. In this blog post, we will explore how to convert Base64 strings to plain text strings in C# using various methods.

Method 1: Using Convert.FromBase64String and Encoding.ASCII.GetString

The first method involves using the Convert.FromBase64String method to convert the Base64 string to a byte array and then using the Encoding.ASCII.GetString method to convert the byte array to a plain text string.

string base64String = "SGVsbG8gV29ybGQ=";
byte[] data = Convert.FromBase64String(base64String);
string plainTextString = Encoding.ASCII.GetString(data);
Console.WriteLine(plainTextString);

Output:

Hello World

In the code above, we first define a Base64 string. We then use the Convert.FromBase64String method to convert the Base64 string to a byte array. Finally, we use the Encoding.ASCII.GetString method to convert the byte array to a plain text string. The resulting plain text string is then printed to the console.

Method 2: Using Convert.FromBase64String and Encoding.UTF8.GetString

The second method is similar to the first method, but instead of using the Encoding.ASCII.GetString method, we use the Encoding.UTF8.GetString method to convert the byte array to a plain text string.

string base64String = "SGVsbG8gV29ybGQ=";
byte[] data = Convert.FromBase64String(base64String);
string plainTextString = Encoding.UTF8.GetString(data);
Console.WriteLine(plainTextString);

Output: 

Hello World

In the code above, we again define a Base64 string and use the Convert.FromBase64String method to convert it to a byte array. We then use the Encoding.UTF8.GetString method to convert the byte array to a plain text string. The resulting plain text string is printed to the console.

Method 3: Using Convert.FromBase64String and StreamReader.ReadToEnd

The third method involves using the Convert.FromBase64String method to convert the Base64 string to a byte array and then using a StreamReader object to read the byte array and convert it to a plain text string.

string base64String = "SGVsbG8gV29ybGQ=";
byte[] data = Convert.FromBase64String(base64String);
string plainTextString;
using (var stream = new MemoryStream(data))
{
    using (var reader = new StreamReader(stream))
    {
        plainTextString = reader.ReadToEnd();
    }
}
Console.WriteLine(plainTextString);

Output: 

Hello World

In the code above, we define a Base64 string and use the Convert.FromBase64String method to convert it to a byte array. We then create a MemoryStream object from the byte array and use a StreamReader object to read the byte array and convert it to a plain text string. The resulting plain text string is printed to the console.

Conclusion:

In this blog post, we explored three different methods for converting Base64 strings to plain text strings in C#. The first two methods involved using the Convert.FromBase64String method to convert the Base64 string to a byte array and then using either the Encoding.ASCII.GetString or Encoding.UTF8.GetString method to convert the byte array to a plain text string. The third method involved using the Convert.FromBase64String method to convert the Base64 string to a byte array and then using a StreamReader object to read the byte array and convert it to a plain text string. All three methods produced the same output of "Hello World".

The choice of method depends on the specific requirements of the application and personal preference. The first two methods are simpler and more straightforward, while the third method provides more control over the conversion process.

Overall, converting Base64 strings to plain text strings in C# is a simple and straightforward process. With the methods presented in this blog post, developers can easily incorporate Base64 decoding functionality into their applications.

Comments (0)

There are no comments. Be the first to comment!!!