C# Encode and Decode String to Base64 with Examples

  By : Suresh Dasari
  Posted On : 23-Apr-2023

Here we will learn what is base64 encoding, how to encode a string to base64 format in c# with examples, and how to decode base64 encoded string back to the original string in c# with examples.

What is Base64?

The base64 is a binary-to-text encoding format that is useful to represent the binary data in an ASCII string format. The base64 character set will consist of alphabets, numbers, and two special characters (+, /). The padding character = may be added to the end of the encoded data to ensure that the length of the encoded string is a multiple of four.

 

The base64 format is useful to convert the binary data to ASCII string format and that can be easily transmitted across the systems as a text stream. Each base64 character represents 6 bits of binary data so to convert binary data to base64 format, first it will split the binary data into 6-bit chunks.

 

If you want to send images or documents across multiple systems, it is always advisable to convert that binary data to base64 encoded string format so that you will not face any data loss problems.

C# Convert String to Base64 Encoded Format

Following is the example of converting the string to a base64 encoded string.

 

using System;
using System.Text;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
        string msg = "Learn C# in Tutlane";

        // Encode the string to base64
        byte[] msgBytes = Encoding.UTF8.GetBytes(msg);
        string base64EncodedString = Convert.ToBase64String(msgBytes);

        Console.WriteLine("Original string: " + msg);
        Console.WriteLine("Encoded string: " + base64EncodedString);
        Console.WriteLine("Press Enter Key to Exit..");
        Console.ReadLine();
      }
   }
}

If you observe the above code, we are converting the given string to byte arrays using System.Text.Encoding.UTF8.GetBytes method. After that, we used System.Convert.ToBase64String method to convert the byte arrays to base64 encoded string.

 

When we execute the above c# example, we will get the result below.

 

C# Encode String to Base64 Format Example Result

 

Now, we will learn how to decode the encoded base64 string to plain text or original string in c# with examples.

C# Decode Base64 String to Original String

Following is the example of decoding the base64 encoded string to the original string/plain text.

 

using System;
using System.Text;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         string msg = "Learn C# in Tutlane";

         // Encode the string to base64
         byte[] msgBytes = Encoding.UTF8.GetBytes(msg);
         string base64EncodedString = Convert.ToBase64String(msgBytes);

         // Decode the base64 string back to the original string
         byte[] base64EncodedBytes = Convert.FromBase64String(base64EncodedString);
         string decodedString = Encoding.UTF8.GetString(base64EncodedBytes);

         Console.WriteLine("Original string: " + msg);
         Console.WriteLine("Encoded string: " + base64EncodedString);
         Console.WriteLine("Decoded string: " + decodedString);
         Console.WriteLine("Press Enter Any Key to Exit..");
         Console.ReadLine();
      }
   }
}

If you observe the above code, we used System.Convert.FromBase64String method to convert the base64 encoded string to byte array. After that, we used System.Text.Encoding.UTF8.GetString method to convert the byte array to the original string.

 

When we execute the above c# example, we will get the result below.

 

C# Decode Base64 String to Original Text Example Result

 

This is how we can encode and decode the string to base64 format in c# using ToBase64String and FromBase64String methods of System.Convert class based on our requirements.