What is Base64 Encoding?
Base64 encoding is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It is commonly used for encoding binary data, such as images or files, for transmission over text-based protocols like email or HTTP. The process involves converting groups of 8 bits into a single ASCII character, resulting in a more compact representation of binary data.
Decoding Base64 in C#:
In C#, to decode Base64 strings using the Convert
class from the System
namespace is simple. The FromBase64String
method is used in this operation.
using System;
class Program
{
static void Main()
{
// Base64 encoded string
string base64String = "SGVsbG8gV29ybGQh";
// Decoding the Base64 string
byte[] decodedBytes = Convert.FromBase64String(base64String);
// Converting the decoded bytes to string
string decodedString = System.Text.Encoding.UTF8.GetString(decodedBytes);
// Displaying the original string
Console.WriteLine("Decoded String: " + decodedString);
}
}
We assigned the Base64-encoded string (SGVsbG8gV29ybGQh
) to the variable and decode it into a byte array, and finally convert it back to a string using UTF-8 encoding. The output shows the decoding of the Base64 string into the original "Hello World!".
Output:
Decoded String: Hello World!
Comments (0)