Convert Byte Array To Hexadecimal String And Vice Versa




Here we consider, how to convert Byte Array to Hexadecimal String. We can make use of BitConverter for doing this job. One typical example is given below for Byte Array to Hexadecimal String conversion.

public string ConvertByteArrayToString( byte[] byteArray )
{
     string hexString = BitConverter.ToString( byteArray );
     return hexString.Replace( "-", "" );
}






Here we consider, how to convert Hexadecimal String To Byte Array. Try following code to convert Hexadecimal String To Byte Array.

public byte[ ] ConvertHexStringToByteArray( String hexString )
{
     int TotalNumberOfChars = hexString.Length;
     byte[ ] bytesArray = new byte[ TotalNumberOfChars / 2 ];
     for ( int i = 0; i < TotalNumberOfChars; i += 2 )
     {
          bytesArray[ i / 2 ] = Convert.ToByte( hexString.Substring(i, 2), 16 );
     }
      return bytesArray;
}