tags:

views:

74

answers:

2
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.

A: 

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'));
}
Donnie DeBoer
A: 

Assuming you're asking how to convert a byte array to a base-2 representation string:

byte b = 123;
string s = Convert.ToString(b, 2); // second argument is base
Console.WriteLine(s); // prints '1111011'

Now just walk through your byte array to create your string byte by byte.

Michael Petrotta