將32位MD5摘要串轉換為128位二進位制字串

FrankYou發表於2015-12-08

將32為MD5摘要串轉換為128位二進位制字串:

 1         /// <summary>
 2         /// 將字串轉成二進位制
 3         /// </summary>
 4         /// <param name="s">源字串</param>
 5         /// <returns>二進位制串</returns>
 6         internal static string ConvertStringToBinary(string s)
 7         {
 8             if (s.IsNullOrWhiteSpace())
 9             {
10                 throw new ArgumentNullException("源字串為空", s);
11             }
12             if (s.Length != 32)
13             {
14                 throw new ArgumentNullException("源字串長度無效", s);
15             }
16             var bitBuilder = new StringBuilder(128);
17             for (var i = 0; i < s.Length; i += 2)
18             {
19                 var tem = Convert.ToInt32(s.Substring(i, 2), 16);
20                 bitBuilder.Append(Convert.ToString(tem, 2).PadLeft(8, '0'));
21             }
22             return bitBuilder.ToString();
23         }

 

相關文章