长度为128的十六进制数,在c#中转换为二进制

时间:2009-12-12 07:41:21

标签: c# binary hex

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.)**********/
}

请看看是否有人可以提供帮助。

2 个答案:

答案 0 :(得分:1)

如果你真的想将哈希的十六进制字符串表示转换为二进制字符串表示:

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'));
}

答案 1 :(得分:-1)

假设您正在询问如何将字节数组转换为base-2表示字符串:

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

现在只需遍历您的字节数组,逐字节创建字符串。