Introduction:
Base64 encoding is a popular technique to convert data to a format that can be easily transmitted over the internet. It is widely used for encoding images, audio, and video files, as well as other types of data. However, sometimes we need to decode the Base64 encoded string to its original form in order to manipulate or use it. In this blog, we will discuss how to convert Base64 decoded strings to normal strings in C#.
Method 1: Using Convert.FromBase64String() method
The first and easiest method to convert Base64 decoded strings to normal strings in C# is by using the Convert.FromBase64String()
method. This method takes a string as input, which contains the Base64 encoded data, and returns an array of bytes that represent the decoded data. We can then convert this byte array to a string using the Encoding class in C#.
Here is a sample code snippet that demonstrates how to use this method:
string base64String = "SGVsbG8gV29ybGQh"; // Sample Base64 encoded string
byte[] base64Bytes = Convert.FromBase64String(base64String); // Decode the Base64 string
string normalString = Encoding.UTF8.GetString(base64Bytes); // Convert the byte array to string
Console.WriteLine(normalString); // Output: "Hello World!"
In this example, we first declare a string variable named base64String
that contains the Base64 encoded string "SGVsbG8gV29ybGQh". We then use the Convert.FromBase64String()
method to decode this string and store the result in the base64Bytes
variable. Finally, we use the Encoding.UTF8.GetString()
method to convert the byte array to a string and store it in the normalString
variable. We then print the output to the console, which is "Hello World!".
Method 2: Using the System.Convert class
Another way to convert Base64 decoded strings to normal strings in C# is by using the System.Convert
class. This class provides a static method named FromBase64String()
that performs the same task as the Convert.FromBase64String()
method. We can then convert the resulting byte array to a string using the Encoding class in C#.
Here is a sample code snippet that demonstrates how to use this method:
string base64String = "SGVsbG8gV29ybGQh"; // Sample Base64 encoded string
byte[] base64Bytes = System.Convert.FromBase64String(base64String); // Decode the Base64 string
string normalString = Encoding.UTF8.GetString(base64Bytes); // Convert the byte array to string
Console.WriteLine(normalString); // Output: "Hello World!"
In this example, we first declare a string variable named base64String
that contains the Base64 encoded string "SGVsbG8gV29ybGQh". We then use the System.Convert.FromBase64String()
method to decode this string and store the result in the base64Bytes
variable. Finally, we use the Encoding.UTF8.GetString()
method to convert the byte array to a string and store it in the normalString
variable. We then print the output to the console, which is "Hello World!".
Method 3: Using the Base64Decode() method from the System.Web namespace
A third method to convert Base64 decoded strings to normal strings in C# is by using the Base64Decode()
method from the System.Web namespace. This method is a static method of the HttpServerUtility
class and can be used to decode a Base64 encoded string. We can then convert the resulting byte array to a string using the Encoding class.
Here is a sample code snippet that demonstrates how to use this method:
string base64String = "SGVsbG8gV29ybGQh"; // Sample Base64 encoded string
byte[] base64Bytes = System.Web.HttpServerUtility.Base64Decode(base64String); // Decode the Base64 string
string normalString = Encoding.UTF8.GetString(base64Bytes); // Convert the byte array to string
Console.WriteLine(normalString); // Output: "Hello World!"
In this example, we first declare a string variable named base64String
that contains the Base64 encoded string "SGVsbG8gV29ybGQh". We then use the System.Web.HttpServerUtility.Base64Decode()
method to decode this string and store the result in the base64Bytes
variable. Finally, we use the Encoding.UTF8.GetString()
method to convert the byte array to a string and store it in the normalString
variable. We then print the output to the console, which is "Hello World!".
Method 4: Using the System.Security.Cryptography namespace
A fourth method to convert Base64 decoded strings to normal strings in C# is by using the System.Security.Cryptography
namespace. This namespace provides the Convert.FromBase64CharArray()
method, which can be used to convert a Base64 encoded string to a byte array. We can then convert the resulting byte array to a string using the Encoding class in C#.
Here is a sample code snippet that demonstrates how to use this method:
string base64String = "SGVsbG8gV29ybGQh"; // Sample Base64 encoded string
char[] base64Chars = base64String.ToCharArray(); // Convert the Base64 string to a char array
byte[] base64Bytes = Convert.FromBase64CharArray(base64Chars, 0, base64Chars.Length); // Decode the Base64 string
string normalString = Encoding.UTF8.GetString(base64Bytes); // Convert the byte array to string
Console.WriteLine(normalString); // Output: "Hello World!"
In this example, we first declare a string variable named base64String
that contains the Base64 encoded string "SGVsbG8gV29ybGQh". We then convert this string to a char array using the ToCharArray()
method and store the result in the base64Chars
variable. We then use the Convert.FromBase64CharArray()
method to decode this char array and store the result in the base64Bytes
variable. Finally, we use the Encoding.UTF8.GetString()
method to convert the byte array to a string and store it in the normalString
variable. We then print the output to the console, which is "Hello World!".
Conclusion:
In this blog post, we discussed several methods to convert Base64 decoded strings to normal strings in C#. We explored using the Convert.FromBase64String()
method, the System.Convert
class, the Base64Decode()
method from the System.Web
namespace, and the Convert.FromBase64CharArray()
method from the System.Security.Cryptography
namespace. All of these methods achieve the same goal and can be used interchangeably depending on the specific use case.
Comments (0)