문제

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!

도움이 되었습니까?

해결책 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}

다른 팁

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

  2. 2 = 2 characters

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top