How To Convert A Byte Array To A String In C#

This code snippet is example of how to convert a byte array to a string using C# and .NET Core and convert a string to a byte array.

This code snippet is an example of how to convert a byte array into a string. String conversion includes two types. First, conversion and display of byte array into a string format, and second, conversion of bytes into actual characters of string.
 
Convert Byte Array To String 
 
The BitConverter class in .NET Framework is provides functionality to convert base datatypes to an array of bytes, and an array of bytes to base data types.
 
The BitConverter class has a static overloaded GetBytes method that takes an integer, double or other base type value and convert that to a array of bytes. The BitConverter class also have other static methods to reverse this conversion. Some of these methods are ToDouble, ToChart, ToBoolean, ToInt16, and ToSingle.
 
The following code snippet converts a byte array into a string. 
  1. string bitString = BitConverter.ToString(bytes);  
The following code snippet converts a byte array into actual character representation of bytes in a string. 
  1. string utfString = Encoding.UTF8.GetString(bytes, 0, bytes.Length);   
Listing 1 is the complete source code. The code is tested in .NET Core 2.2 and C#.
  1. using System;  
  2. using System.Text;  
  3. using System.Security.Cryptography;  
  4.   
  5. class Program  
  6. {  
  7.     static void Main(string[] args)  
  8.     {  
  9.         string plainData = "Mahesh Chand is the founder of C# Corner and an author and speaker.";  
  10.         Console.WriteLine("Raw data: {0}", plainData);  
  11.     
  12.         // Sha1  
  13.         Console.WriteLine("============SHA1 Hash=============");  
  14.   
  15.         // Create SHA1Managed  
  16.         using (SHA1Managed sha1 = new SHA1Managed())  
  17.         {  
  18.             // ComputeHash - returns byte array  
  19.             byte[] bytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(plainData));  
  20.   
  21.             // Merge all bytes into a string of bytes  
  22.             StringBuilder builder = new StringBuilder();  
  23.             for (int i = 0; i < bytes.Length; i++)  
  24.             {  
  25.                 builder.Append(bytes[i].ToString("x2"));  
  26.             }  
  27.             Console.WriteLine(builder.ToString());  
  28.   
  29.             // BitConverter can also be used to put all bytes into one string  
  30.             string bitString = BitConverter.ToString(bytes);  
  31.             Console.WriteLine(bitString);  
  32.   
  33.             // UTF conversion - String from bytes  
  34.             string utfString = Encoding.UTF8.GetString(bytes, 0, bytes.Length);  
  35.             Console.WriteLine(utfString);  
  36.   
  37.             // ASCII conversion - string from bytes  
  38.             string asciiString = Encoding.ASCII.GetString(bytes, 0, bytes.Length);  
  39.             Console.WriteLine(asciiString);              
  40.         }     
  41.         Console.ReadLine();  
  42.     }  
  43. }  
Listing 1.
 
Figure 1 is the output of Listing 1. 
 
 
 
Figure 1.