This post will discuss how to convert a byte array to a string in C#.

1. Using Encoding.GetString() method

To decode all bytes in the byte array into a string, use the Encoding.GetString() method. Several decoding schemes are available in Encoding class – UTF8, Unicode, UTF32, ASCII, etc.

Download  Run Code

2. Using Convert.ToBase64String() method

To decode the bytes encoded with base-64 digits, use the Convert.ToBase64String() method. This is demonstrated below:

Download  Run Code

3. Using MemoryStream Class

Here, the idea is to create the byte stream from a specified byte array. Then read all characters from the byte stream and return the stream as a string.

The following code example shows how to implement this. The solution automatically tries to determine the encoding used using the byte order mark (BOM) in the byte stream. If not found, UTF-8 encoding is assumed.

Download  Run Code

That’s all about converting a byte array to a string in C#.