Convert String to Base64

Here are two simple functions to convert String value to Base64 format and vice versa.

//String to Base64

public static string StringToBase64(string str)
byte[] b = System.Text.Encoding.UTF8.GetBytes(str);
string b64 = Convert.ToBase64String(b);
return b64;

/ /Base64string to string
public static string Base64ToString(string b64)
byte[] b = Convert.FromBase64String(b64);

and this function converts the string value to Byte array.

public static byte[] convertStringToBytes(string str)
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);


One thought on “Convert String to Base64

  1. Linda Smith says:

    once I convert the string fromBase65. how do I
    display it in a control on a web page using c#.

    Thank You

    Hey Linda,

    The return type of value from Base64 is string. so you can assign it to the text property of the Server Side controls. For example, if you want it to display in a Label control, just do as follows

    Label1.Text = Base64ToString(string b64);

    string baseTostr = Base64ToString(string b64);
    Label1.Text = baseTostr;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.