Mohanapriya R Mohanapriya R
Updated date Jul 12, 2024
In this article, we will learn how to convert strings to Base64 encoding in C#.
  • 4.2k
  • 0
  • 0

How to Convert Strings to Base64 Encode in C#?

In C#, the Convert class provides a simple method for encoding strings to Base64. The Convert.ToBase64String method takes a byte array as an argument and returns the Base64-encoded string. 

using System;

class Program
{
    static void Main()
    {
        // Input string
        string originalString = "Hello, TechieHook!";

        // Convert the string to a byte array
        byte[] bytesToEncode = System.Text.Encoding.UTF8.GetBytes(originalString);

        // Perform Base64 encoding
        string base64String = Convert.ToBase64String(bytesToEncode);

        // Display the original and Base64-encoded strings
        Console.WriteLine("Original String: " + originalString);
        Console.WriteLine("Base64 Encoded String: " + base64String);
        Console.ReadKey();
    }
}

Output:

 

Original String: Hello, TechieHook!
Base64 Encoded String: SGVsbG8sIFRlY2hpZUhvb2sh

Comments (0)

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