How to convert byte array to a hexadecimal string in C#, and vice versa?

convert byte array to a hexadecimal string in C#

This post contains two simple methods to convert byte array to hexadecimal string in C# and vice versa.

public static string ByteArrayToHexadecimalString(byte[] byteArray)
{
  StringBuilder hexadecimalString = new StringBuilder(byteArray.Length * 2);
  foreach (byte b in byteArray){
     hexadecimalString.AppendFormat("{0:x2}", b);
  }
  return hexadecimalString.ToString();
}

or we can use BitConverter Class

public static string ByteArrayToHexadecimalString(byte[] byteArray)
{
  return BitConverter.ToString(byteArray).Replace("-","");
}

To convert hexa decimal string to byte array in c# use the below method

public static byte[] HexadecimalStringToByteArray(String hexadecimalString)
{
  int length = hexadecimalString.Length;
  byte[] byteArray = new byte[length / 2];
  for (int i = 0; i < length; i += 2){
    byteArray[i / 2] = Convert.ToByte(hexadecimalString.Substring(i, 2), 16);
  }
  return byteArray;
}
Spread the word

Get free link to download 900+ Material Icons
Avatar
Arunkumar Gudelli

I am One among a million Software engineers of India. I write beautiful markup.I make the Web useful.

Related

Previous