【问题标题】:What does these mean "{0,2:X2}" in statement String.Format("{0,2:X2}", b);语句中的“{0,2:X2}”是什么意思 String.Format("{0,2:X2}", b);
【发布时间】:2013-02-26 09:57:43
【问题描述】:

代码:

        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() 的参数,但无法准确理解。

提前致谢!

【问题讨论】:

    标签: asp.net sha1 string.format


    【解决方案1】:
    Formatting the string in hexadecimal format...
    
    1. X = 十六进制格式

    2. 2 = 2 个字符

    【讨论】:

      【解决方案2】:

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

      十六进制(“X”)格式说明符将数字转换为十六进制数字字符串。格式说明符的大小写表示对于大于 9 的十六进制数字是使用大写还是小写字符。

      这种特殊的格式称为Composite Formatting,因此分解一下:

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

      【讨论】:

      • 在这种特殊情况下,,2 部分(对齐)不会改变任何内容,因为X2 格式保证生成长度为至少两个的字符串,因此两个对齐不会改变任何事情。但在其他情况下,它可能很有用。
      猜你喜欢
      • 2014-01-12
      • 2016-07-08
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-18
      • 2015-06-04
      相关资源
      最近更新 更多