String to Base64 [C#]

string to base64

In this post we will look how to convert string to Base64 using C#. .Net provides .ToBase64String() method in System.Convert class that we can use to convert string to base64. It basically converts value of an array of 8-bit unsigned integers to its equivalent String representation consisting of base 64 digits.

Converting string to Base64 is bit tricky. We need to follow some steps mentioned as below

  • 1. We need to convert string to byte array.
  • 2. then we need to use Convert.ToBase64String() to convert byte array to base64 string.

Sample code to convert string to Base64 string [C#]

str ="How to convert string to nase64 in C#"; 
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(str);
// Now convert the byte array to a Base64 string
strBase64 = Convert.ToBase64String(byteArray);