C# – hex number of length 128 which is to be converted to binary in c#

binarychex

private static string GetSHA512(string strPlain)
{
     UnicodeEncoding UE = new UnicodeEncoding();
     byte[] HashValue, MessageBytes = UE.GetBytes(strPlain);
     SHA512Managed SHhash = new SHA512Managed();
     string strHex = "";

      HashValue = SHhash.ComputeHash(MessageBytes);

      foreach (byte b in HashValue)
      {
          strHex += String.Format("{0:x2}", b);
          //strHex += b.ToString();
      }
      int len = strHex.Length;

      //********This strHex of length 128 characters is to be converted to binary
      // ( actually 512 bit output in binary is required.)**********/
}

please see if anyone can help.

Best Answer

If you really want to convert the hex string representation of the hash to a binary string representation:

int len = strHex.Length;
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < len; i++)
{
    sb.Append(Convert.ToString(Convert.ToByte(strHex.Substring(i, 1), 16), 2).PadLeft(4, '0'));
}
Related Topic