Converting a md5 hash byte array to a string

前端 未结 7 1613
余生分开走
余生分开走 2021-01-31 13:28

How can I convert the hashed result, which is a byte array, to a string?

byte[] bytePassword = Encoding.UTF8.GetBytes(password);

using (MD5 md5 = MD5.Create())
         


        
7条回答
  •  萌比男神i
    2021-01-31 14:03

       public static string ToHex(this byte[] bytes, bool upperCase)
        {
            StringBuilder result = new StringBuilder(bytes.Length*2);
    
            for (int i = 0; i < bytes.Length; i++)
                result.Append(bytes[i].ToString(upperCase ? "X2" : "x2"));
    
            return result.ToString();
        }
    

    You can then call it as an extension method:

    string hexString = byteArray.ToHex(false);
    

提交回复
热议问题