I'm studying MD5 encryption, and have found this code using Google:
public string CalculateMD5Hash(string input)
{
// Primeiro passo, calcular o MD5 hash a partir da string
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
// Segundo passo, converter o array de bytes em uma string haxadecimal
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
Why does it use ToString("X2")
? How is it different from normal ToString
?
This question is related to
c#
encryption
tostring
ToString("X2") prints the input in Hexadecimal
It prints the byte
in Hexadecimal format.
No format string: 13
'X2' format string: 0D
http://msdn.microsoft.com/en-us/library/aa311428(v=vs.71).aspx
Source: Stackoverflow.com