Sai A Sai A
Updated date May 22, 2023
In this blog, we will provide a comprehensive guide on how to convert byte array to string in C#. It covers four commonly used methods for converting byte array to string, including Encoding.ASCII.GetString(), Encoding.UTF8.GetString(), Convert.ToBase64String(), and BitConverter.ToString(). Each method is explained in detail, and sample programs are provided to demonstrate how they work.

Introduction:

In C#, byte arrays are used to store binary data. However, there are instances where we need to convert these byte arrays to strings, such as when we want to transmit the data over the network or store it in a text file. There are several methods available in C# for converting byte arrays to strings, and in this blog, we will discuss some of the most commonly used methods along with their advantages and disadvantages.

Method 1: Encoding.ASCII.GetString()

The Encoding.ASCII.GetString() method is a built-in method provided by C# that converts a byte array to a string using ASCII encoding. This method is simple and easy to use, and it is suitable for scenarios where the byte array only contains ASCII characters.

Here's an example program that demonstrates how to use this method:

byte[] byteArray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 };
string str = Encoding.ASCII.GetString(byteArray);
Console.WriteLine(str);

Output: 

Hello World

Method 2: Encoding.UTF8.GetString()

The Encoding.UTF8.GetString() method is another built-in method provided by C# that converts a byte array to a string using the UTF-8 encoding. UTF-8 is a widely used encoding format that can handle a wide range of characters, including non-ASCII characters.

Here's an example program that demonstrates how to use this method:

byte[] byteArray = { 240, 159, 145, 132, 240, 159, 145, 133 };
string str = Encoding.UTF8.GetString(byteArray);
Console.WriteLine(str);

Output:

👌👍

Method 3: Convert.ToBase64String()

The Convert.ToBase64String() method is a built-in method provided by C# that converts a byte array to a Base64-encoded string. Base64 encoding is a technique used to convert binary data into a string format that can be transmitted over the network or stored in a text file.

Here's an example program that demonstrates how to use this method:

byte[] byteArray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 };
string str = Convert.ToBase64String(byteArray);
Console.WriteLine(str);

Output: 

SGVsbG8gV29ybGQ=

Method 4: BitConverter.ToString()

The BitConverter.ToString() method is a built-in method provided by C# that converts a byte array to a string of hexadecimal values. This method is useful for scenarios where we need to represent binary data in a human-readable format.

Here's an example program that demonstrates how to use this method:

byte[] byteArray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 };
string str = BitConverter.ToString(byteArray).Replace("-", "");
Console.WriteLine(str);

Output:

48656C6C6F20576F726C64

Conclusion:

In this blog, we have discussed four methods for converting byte arrays to strings in C#. These methods include Encoding.ASCII.GetString(), Encoding.UTF8.GetString(), Convert.ToBase64String(), and BitConverter.ToString(). Each of these methods has its own advantages and disadvantages, and the choice of method depends on the specific scenario and requirements of the application.

Comments (0)

There are no comments. Be the first to comment!!!