How To Convert Byte Array To String In C#

In this article, we will learn how to convert byte array to string in C#

String conversion includes two types. First, conversion and display of C# byte array into a string format, and second, conversion of C# bytes into actual characters of string.

The BitConverter class also have other static methods to reverse this conversion. Some of these methods are ToDouble, ToChart, ToBoolean, ToInt16, and ToSingle.

First converts a byte array into a string.

string bitString = BitConverter.ToString(bytes);

Below code onverts a byte array into actual character representation of bytes in a string.

string utfString = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
using System;  
using System.Text;  
using System.Security.Cryptography;  
class Program  
    static void Main(string[] args)  
        string plainData = "Mahesh Chand is the founder of C# Corner and an author and speaker.";  
        Console.WriteLine("Raw data: {0}", plainData);  
        // Sha1  
        Console.WriteLine("============SHA1 Hash=============");  
        // Create SHA1Managed  
        using (SHA1Managed sha1 = new SHA1Managed())  
            // ComputeHash - returns byte array  
            byte[] bytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(plainData));  
            // Merge all bytes into a string of bytes  
            StringBuilder builder = new StringBuilder();  
            for (int i = 0; i < bytes.Length; i++)  
            // BitConverter can also be used to put all bytes into one string  
            string bitString = BitConverter.ToString(bytes);  
            // UTF conversion - String from bytes  
            string utfString = Encoding.UTF8.GetString(bytes, 0, bytes.Length);  
            // ASCII conversion - string from bytes  
            string asciiString = Encoding.ASCII.GetString(bytes, 0, bytes.Length);  



Hope you understand the article , If you still have any questions or queries then please let me know in the comment section, I’ll respond to you as soon as possible.

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories