汉字转拼音

来源:百度文库 编辑:神马文学网 时间:2024/04/28 20:52:19
  1. using System;   
  2. using System.Configuration;   
  3. using System.Data;   
  4. using System.Web;   
  5. using System.Web.Security;   
  6. using System.Web.UI;   
  7. using System.Web.UI.HtmlControls;   
  8. using System.Web.UI.WebControls;   
  9. using System.Web.UI.WebControls.WebParts;   
  10.   
  11. public partial class _Default : System.Web.UI.Page   
  12. {   
  13.     protected void Page_Load(object sender, EventArgs e)   
  14.     {   
  15.         Response.Write(GetPYString("中华人民共和国"));   
  16.     }   
  17.     public string GetPYString(string str)   
  18.     {   
  19.         string tempStr = "";   
  20.         foreach (char c in str)   
  21.         {   
  22.             if ((int)c >= 33 && (int)c <= 126)   
  23.             {//字母和符号原样保留    
  24.                 tempStr += c.ToString();   
  25.             }   
  26.             else  
  27.             {//累加拼音声母    
  28.                 tempStr += GetPYChar(c.ToString());   
  29.             }   
  30.         }   
  31.         return tempStr;   
  32.     }   
  33.     ///     
  34.     /// 取单个字符的拼音声母    
  35.     ///     
  36.     /// 要转换的单个汉字    
  37.     /// 拼音声母    
  38.     public string GetPYChar(string c)   
  39.     {   
  40.         byte[] array = new byte[2];   
  41.         array = System.Text.Encoding.Default.GetBytes(c);   
  42.         int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));   
  43.         if (i < 0xB0A1) return "*";   
  44.         if (i < 0xB0C5) return "a";   
  45.         if (i < 0xB2C1) return "b";   
  46.         if (i < 0xB4EE) return "c";   
  47.         if (i < 0xB6EA) return "d";   
  48.         if (i < 0xB7A2) return "e";   
  49.         if (i < 0xB8C1) return "f";   
  50.         if (i < 0xB9FE) return "g";   
  51.         if (i < 0xBBF7) return "h";   
  52.         if (i < 0xBFA6) return "g";   
  53.         if (i < 0xC0AC) return "k";   
  54.         if (i < 0xC2E8) return "l";   
  55.         if (i < 0xC4C3) return "m";   
  56.         if (i < 0xC5B6) return "n";   
  57.         if (i < 0xC5BE) return "o";   
  58.         if (i < 0xC6DA) return "p";   
  59.         if (i < 0xC8BB) return "q";   
  60.         if (i < 0xC8F6) return "r";   
  61.         if (i < 0xCBFA) return "s";   
  62.         if (i < 0xCDDA) return "t";   
  63.         if (i < 0xCEF4) return "w";   
  64.         if (i < 0xD1B9) return "x";   
  65.         if (i < 0xD4D1) return "y";   
  66.         if (i < 0xD7FA) return "z";   
  67.         return "*";   
  68.     }   
  69. }