2013-02-26 554 views
-2

代码:在语句String.Format(“{0,2:X2}”,b)中是什么意思“{0,2:X2}”;

 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); 
     } 

我搜索传递到的String.Format()参数但没有能够正确理解它。

在此先感谢!

回答

2

它基本上只是以大写十六进制格式格式化字符串 - 请参阅the docs

十六进制(“X”)格式说明符将数字转换为十六进制数字的字符串。格式说明的情况下,指示是否要用于十六进制数字是大于9。

这种特殊的格式称为Composite Formatting大写或小写字符,所以把它分解:

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

谢谢..非常丰富链路.. – user2109951 2013-02-28 10:17:36

+0

在此特定情况下,',因为'X2'格式是保证产生长度_at least_的字符串2'部(对齐)没有任何改变两个,所以两个不同的东西不会改变任何东西。但在其他情况下,它可能是有用的。 – 2013-03-28 00:37:18

3
Formatting the string in hexadecimal format... 
  1. X =十六进制格式

  2. 2 = 2个字符

相关问题