How to convert string to base64 in C#
By FoxLearn 12/16/2024 7:29:41 AM 86
In C#, you can convert a string to a Base64 encoded string using the Convert.ToBase64String method.
C# Convert String to Base64
First, we convert the string into a byte array using the Encoding.UTF8.GetBytes
method, then use Convert.ToBase64String
to convert the byte array to a Base64 string.
// c# convert string to base64 public static string ConvertStringToBase64(string value) { // Step 1: Convert the string to a byte array byte[] byteArray = Encoding.UTF8.GetBytes(value); // Step 2: Convert the byte array to a Base64 encoded string string base64String = Convert.ToBase64String(byteArray); return base64String; }
Main method.
using System; using System.Text; class Program { static void Main() { string str = "C# Programming"; string base64String = ConvertStringToBase64(str); // Output the Base64 encoded string Console.WriteLine("Base64 Encoded: " + base64String); } }
Output:
Base64 Encoded: QyMgUHJvZ3JhbW1pbmc=
This code converts "
C# Programming"
to "QyMgUHJvZ3JhbW1pbmc="
, which is the Base64 encoded representation of the string.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#
Categories
Popular Posts
Freedash bootstrap lite
11/13/2024
Dashboardkit Bootstrap
11/13/2024
Material Lite Admin Template
11/14/2024
Stisla Admin Dashboard Template
11/18/2024