標籤:winform style blog io color ar os 使用 sp
在學習三層架構時,我們在需要擷取中文字所擷取的拼音,需要引進一個ChnCharInfo.dll的程式檔案,並且引用命名空間
using Microsoft.International.Converters.PinYinConverter;
接下來是如何?拼音的擷取:
1 public static string GetPinyins(string name) 2 { 3 //進行拼接字串 4 StringBuilder sb = new StringBuilder(); 5 //由於ChineseChar中必須為char類型,所以我們進行字串遍曆成char類型 6 foreach (char item in name) 7 { 8 //判斷得到的字元是否為合法的中文字元 9 if (ChineseChar.IsValidChar(item))10 {11 //如果是合法的,那麼執行下面的語句12 ChineseChar c = new ChineseChar(item);13 //根據傳入的字元得到拼音14 ReadOnlyCollection<string> ps = c.Pinyins;15 //拼接字串,這裡說明一下:由於拼音是有聲韻的,在這裡得到的拼音也是包含音調,即ps的最後一個字元16 //所以我們要截取字串,保留前面的17 sb.Append(ps[0].Substring(0, ps[0].Length - 1));18 }19 else20 {21 //如果為不合法的,不用擷取,直接拼接22 sb.Append(item);23 }24 }25 //返回得到的拼接字串26 return sb.ToString();27 }
那怎麼擷取對密碼加密呢?
首先也要引用:
using System.Security.Cryptography;
實現:
1 public static string GetMD5(string pwd) 2 { 3 //這裡我們使用的Winform內建的MD5加密 4 //這裡的MD5為私人的不可訪問的,不過提供了一個可以訪問的公用的方法進行建立 5 MD5 md5 = MD5.Create(); 6 //準備拼接字串 7 StringBuilder sb = new StringBuilder(); 8 //通過使用者傳入的密碼得到byte數組 9 byte[] bytes = Encoding.Default.GetBytes(pwd);10 //將得到的byte數組進行計算Hash碼11 byte[] newBytes = md5.ComputeHash(bytes);12 //遍曆每一個Hash碼進行16進位的轉換13 foreach (byte item in newBytes)14 {15 //將Hash進行16進位格式的轉換16 sb.Append(item.ToString("X2"));17 }18 //返回得到的拼接字串,即得到32位的加密密文19 return sb.ToString();20 }
C#三層架構(擷取中文拼音和給密碼加密)