Question

Code:

        SHA1 sha = new SHA1CryptoServiceProvider();
        string hashedValue = string.Empty;
        //hash the data
        byte[] hashedData = sha.ComputeHash(Encoding.Unicode.GetBytes(str));

        //loop through each byte in the byte array
        foreach (byte b in hashedData)
        {
            //convert each byte and append
            hashedValue += String.Format("{0,2:X2}", b);
        }

I searched for arguments passed to String.Format() but didnt able to understand it exactly.

Thanks in advance!

Was it helpful?

Solution 2

It's basically just formatting the string in uppercase hexadecimal format - see the docs.

The hexadecimal ("X") format specifier converts a number to a string of hexadecimal digits. The case of the format specifier indicates whether to use uppercase or lowercase characters for hexadecimal digits that are greater than 9.

This particular format is known as Composite Formatting, so to break it down:

{0 = parameterIndex, 2 = alignment :X2 = formatString}

OTHER TIPS

Formatting the string in hexadecimal format...
  1. X = Hexadecimal format

  2. 2 = 2 characters

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top