Convert Base64 text into String in C# – vice versa

In this article, learn how to convert string into Base64 format and converting Base64 text back into string format.


Converting Base64 text to String:

Use the C# inbuilt function System.Convert.ToBase64String() to convert string to Base64 txt.

using System.Data.Odbc;
/// 
/// Function to convert or encode any text into Base64 format.
/// 
/// any plane text
/// The Base64 text
public static string Base64Encode(string plainText)
{
     //Get bytes
     byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);

     //Convert to Base64 to string.
     return System.Convert.ToBase64String(plainTextBytes);
}

Converting String into Base64 text:

Use the C# inbuilt function System.Convert.ToBase64String() to convert Base64 text into string.

using System.Data.Odbc;
/// 
/// Function to convert or decode any Base64 text into string format.
/// 
/// any Base64 text
/// the string text
public static string Base64Decode(string sBase64Text)
{
     //Get bytes of Base64 string.
     byte[] inputBase64InBytes = Convert.FromBase64String(sBase64Text);

     //Convert Bytes to string.
     return System.Text.Encoding.UTF8.GetString(inputBase64InBytes); 
}

Complete code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
			string sInputString = "Hello World";
			
			string sBase64Output = Base64Encode(sInputString);  //"SGVsbG8gV29ybGQ=" is returned.
            Console.WriteLine(string.Format("'{0}' into Base64 format: {1}", sInputString,sBase64Output));

            string sStringOutput = Base64Decode(sBase64Output); 
            Console.WriteLine(string.Format("Converted Base64 text '{0}' into string is: {1}", sBase64Output, sStringOutput));

            Console.ReadLine();
        }

        /// 
        /// Function to convert or encode any text into Base64 format.
        /// 
        /// any plane text
        /// The Base64 text
        public static string Base64Encode(string plainText)
        {
            //Get bytes
            byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);

            //Convert to Base64 to string.
            return System.Convert.ToBase64String(plainTextBytes);
        }

        /// 
        /// Function to convert or decode any Base64 text into string format.
        /// 
        /// any Base64 text
        /// the string text
        public static string Base64Decode(string sBase64Text)
        {
            //Get bytes of Base64 string.
            byte[] inputBase64InBytes = Convert.FromBase64String(sBase64Text);

            //Convert Bytes to string.
            return System.Text.Encoding.UTF8.GetString(inputBase64InBytes); 
        }
    }
}


Try yourself:


– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this:
We will be happy to hear your thoughts

      Leave a reply

      www.troubleshootyourself.com
      Logo