1 private void button1_Click(object sender, EventArgs e) 2 { 3 var value = this.textBox1.Text; 4 var aa = GetChineseSpell(value); 5 this.textBox2.Text = aa; 6 } 7 8 public string GetChineseSpell(string strText) 9 { 10 int len = strText.Length; 11 string myStr = ""; 12 for (int i = 0; i < len; i++) 13 { 14 myStr += getSpell(strText.Substring(i, 1)); 15 } 16 return myStr; 17 } 18 19 public string getSpell(string cnChar) 20 { 21 byte[] arrCN = Encoding.Default.GetBytes(cnChar); 22 if (arrCN.Length > 1) 23 { 24 int area = (short)arrCN[0]; 25 int pos = (short)arrCN[1]; 26 int code = (area << 8) + pos; 27 int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 }; 28 for (int i = 0; i < 26; i++) 29 { 30 int max = 55290; 31 if (i != 25) max = areacode[i + 1]; 32 if (areacode[i] <= code && code < max) 33 { 34 return Encoding.Default.GetString(new byte[] { (byte)(65 + i) }); 35 } 36 } 37 return "*"; 38 } 39 else return cnChar; 40 }
原文:http://www.cnblogs.com/paihuai/p/5314239.html