Mohanapriya R Mohanapriya R
Updated date Jul 12, 2024
In this article, we will learn how to decode Base64-encoded strings into human-readable text using C#.

Decoding Base64 to Strings in C#:

In C#, decoding Base64-encoded data is straightforward, thanks to the Convert class provided by the .NET framework. The Convert class includes methods like FromBase64String, that takes a Base64-encoded string as input and return the corresponding byte array.

using System;

class Program
{
    static void Main()
    {
        // Base64-encoded string
        string base64String = "SGVsbG8sIFRlY2hpZUhvb2sh";

        // Convert Base64 to byte array
        byte[] byteArray = Convert.FromBase64String(base64String);

        // Convert byte array to string
        string decodedString = System.Text.Encoding.UTF8.GetString(byteArray);

        // Output the decoded string
        Console.WriteLine("Decoded String: " + decodedString);
        Console.ReadKey();
    }
}

We started with a Base64-encoded string ("SGVsbG8sIFRlY2hpZUhvb2sh"). We used Convert.FromBase64String to obtain a byte array, and then converted this byte array to a string using System.Text.Encoding.UTF8.GetString.

Output:

Decoded String: Hello, TechieHook!

 

Comments (0)

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