2013-02-26 552 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個字符

相關問題