128

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?

live2
  • 3,771
  • 2
  • 37
  • 46
Lai32290
  • 8,062
  • 19
  • 65
  • 99

3 Answers3

157

It formats the string as two uppercase hexadecimal characters.

In more depth, the argument "X2" is a "format string" that tells the ToString() method how it should format the string. In this case, "X2" indicates the string should be formatted in Hexadecimal.

byte.ToString() without any arguments returns the number in its natural decimal representation, with no padding.

Microsoft documents the standard numeric format strings which generally work with all primitive numeric types' ToString() methods. This same pattern is used for other types as well: for example, standard date/time format strings can be used with DateTime.ToString().

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • 8
    In case anyone has read Clean Code and is thinking ... "could we just get this named ToHexString() in the framework" ... and we can all stop writing our own extension functions? X2 doesn't just roll of the brain for some of us. Maybe the format string "2HX" but that's just trying to be cute. – ebol2000 Mar 25 '19 at 19:12
  • 2
    format as MINIMUM of two uppercase hex characters. If there are more than two it will return all of them. so F would return as 0F but FFF would return as FFF. – Tomislav3008 May 14 '19 at 10:35
  • 1
    Yep "X2" provides two values that can be controlled separately as needed. The 2 specifies the minimum number of characters to return, while the X specifies to output in Hex according to its case. So while X4 might produce 00FF, x4 would produce 00ff. – Keith Langmead Oct 26 '21 at 15:08
  • Just for the sake of clarity. Two hex digits represent 256 bits which is the equivalent to 1 byte. So it's a naturally elegant pairing. In the case of a byte array, it works fantastically. Using a [StringBuilder](https://learn.microsoft.com/en-us/dotnet/standard/base-types/stringbuilder), one can output a shortened hexadecimal string representation of something like the output of a hashing function. – Jason Mar 13 '23 at 10:37
61

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

Community
  • 1
  • 1
Dave Zych
  • 21,581
  • 7
  • 51
  • 66
18

ToString("X2") prints the input in Hexadecimal

Dominic B.
  • 1,897
  • 1
  • 16
  • 31