方法一:
1. /// <summary>
2. /// 生成拼音简码
3. /// </summary>
4. /// <param name="unicodeString">Unicode编码字符串</param>
5. /// <returns>拼音简码:string</returns>
6. public static string GetPinyinCode(string unicodeString)
7. {
8. int i = 0;
9. ushort key = 0;
10. string strResult = string.Empty; //创建两个不同的encoding对象
11. Encoding unicode = Encoding.Unicode;
12. //创建GBK码对象
13. Encoding gbk = Encoding.GetEncoding(936);
14. //将unicode字符串转换为字节
15. byte[] unicodeBytes = unicode.GetBytes(unicodeString);
16. //再转化为GBK码
17. byte[] gbkBytes = Encoding.Convert(unicode, gbk, unicodeBytes);
18. while (i < gbkBytes.Length)
19. {
20. //如果为数字\字母\其他ASCII符号
21. if (gbkBytes <= 127)
22. {
23. strResult = strResult + (char)gbkBytes;
24. i++;
25. }
26. #region 否则生成汉字拼音简码,取拼音首字母
27. else
28. {
29. key = (ushort)(gbkBytes * 256 + gbkBytes[i + 1]);
30. if (key >= '\uB0A1' && key <= '\uB0C4')
31. {
32. strResult = strResult + "A";
33. }
34. else if (key >= '\uB0C5' && key <= '\uB2C0')
35. {
36. strResult = strResult + "B";
37. }
38. else if (key >= '\uB2C1' && key <= '\uB4ED')
39. {
40. strResult = strResult + "C";
41. }
42. else if (key >= '\uB4EE' && key <= '\uB6E9')
43. {
44. strResult = strResult + "D";
45. }
46. else if (key >= '\uB6EA' && key <= '\uB7A1')
47. {
48. strResult = strResult + "E";
49. }
50. else if (key >= '\uB7A2' && key <= '\uB8C0')
51. {
52. strResult = strResult + "F";
53. }
54. else if (key >= '\uB8C1' && key <= '\uB9FD')
55. {
56. strResult = strResult + "G";
57. }
58. else if (key >= '\uB9FE' && key <= '\uBBF6')
59. {
60. strResult = strResult + "H";
61. }
62. else if (key >= '\uBBF7' && key <= '\uBFA5')
63. {
64. strResult = strResult + "J";
65. }
66. else if (key >= '\uBFA6' && key <= '\uC0AB')
67. {
68. strResult = strResult + "K";
69. }
70. else if (key >= '\uC0AC' && key <= '\uC2E7')
71. {
72. strResult = strResult + "L";
73. }
74. else if (key >= '\uC2E8' && key <= '\uC4C2')
75. {
76. strResult = strResult + "M";
77. }
78. else if (key >= '\uC4C3' && key <= '\uC5B5')
79. {
80. strResult = strResult + "N";
81. }
82. else if (key >= '\uC5B6' && key <= '\uC5BD')
83. {
84. strResult = strResult + "O";
85. }
86. else if (key >= '\uC5BE' && key <= '\uC6D9')
87. {
88. strResult = strResult + "P";
89. }
90. else if (key >= '\uC6DA' && key <= '\uC8BA')
91. {
92. strResult = strResult + "Q";
93. }
94. else if (key >= '\uC8BB' && key <= '\uC8F5')
95. {
96. strResult = strResult + "R";
97. }
98. else if (key >= '\uC8F6' && key <= '\uCBF9')
99. {
100. strResult = strResult + "S";
101. }
102. else if (key >= '\uCBFA' && key <= '\uCDD9')
103. {
104. strResult = strResult + "T";
105. }
106. else if (key >= '\uCDDA' && key <= '\uCEF3')
107. {
108. strResult = strResult + "W";
109. }
110. else if (key >= '\uCEF4' && key <= '\uD188')
111. {
112. strResult = strResult + "X";
113. }
114. else if (key >= '\uD1B9' && key <= '\uD4D0')
115. {
116. strResult = strResult + "Y";
117. }
118. else if (key >= '\uD4D1' && key <= '\uD7F9')
119. {
120. strResult = strResult + "Z";
121. }
122. else
123. {
124. strResult = strResult + "?";
125. }
126. i = i + 2;
127. }
128. #endregion
129. }//end while
130. return strResult;
131. }
原文地址:http://qzone.qq.com/blog/47541785-1223276474
方法二:
1. static public string GetChineseSpell(string strText)
2. {
3. int len = strText.Length;
4. string myStr = "";
5. for(int i=0;i<len;i++)
6. {
7. myStr += getSpell(strText.Substring(i,1));
8. }
9. return myStr;
10. }
11.
12. static public string getSpell(string cnChar)
13. {
14. byte[] arrCN = Encoding.Default.GetBytes(cnChar);
15. if(arrCN.Length > 1)
16. {
17. int area = (short)arrCN[0];
18. int pos = (short)arrCN[1];
19. int code = (area<<8) + pos;
20. 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};
21. for(int i=0;i<26;i++)
22. {
23. int max = 55290;
24. if(i != 25) max = areacode[i+1];
25. if(areacode[i]<=code && code<max)
26. {
27. return Encoding.Default.GetString(new byte[]{(byte)(65+i)});
28. }
29. }
30. return "*";
31. }
32. else return cnChar;
33. }