Byte array to string c sharp


While storing hash or encoded byte array in database we convert it to string which makes it simple to compare to other records. Generally you can convert byte array to hex string, base 64 encoded string or regular string.

1. Byte[] to hex string:

string hex = BitConverter.ToString(data);  // Here is data is byte[]

2. Byte[] to base 64 string

string base64 = Convert.ToBase64String(data);

3. Byte[] to string using System.Text

string s = System.Text.Encoding.UTF8.GetString(data);

4. Byte[] to string  using StringBuilder

StringBuilder sb = new StringBuilder(data.Length*2);

foreach (byte b in data)

sb.Append(b.ToString("X2")); return sb.ToString();

Leave a Reply

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