Decoding Base64 in C#
To decode Base64 strings using the Convert
class in the System
namespace. The FromBase64String
method takes a Base64 encoded string as input and returns the original byte array. From there, we can convert the byte array into a normal string using the Encoding
class.
using System;
using System.Text;
class Program
{
static void Main()
{
// Base64 encoded string
string base64String = "SGVsbG8gd29ybGQh";
// Decode Base64 string
byte[] bytes = Convert.FromBase64String(base64String);
// Convert byte array to string
string result = Encoding.UTF8.GetString(bytes);
// Output the result
Console.WriteLine("Decoded String: " + result);
}
}
Output:
Decoded String: Hello world!
In this example, the Base64 encoded string "SGVsbG8gd29ybGQh" is successfully decoded back to its original form, "Hello world!".
Comments (0)