话不多说,直接上代码,代码参考网上源码.其中存在X显示为?的问题.一共两种写法.目前该写法还是存在部分汉字无法正常转化的问题.另外一种方法在另一个帖子中说明.
帖子链接 : https://www.cnblogs.com/dygood/p/12015120.html
/// <summary> /// 将输入字符串英文部分变为大写,中文取拼音首字母大写. /// </summary> /// <param name="UnName"></param> /// <returns></returns> public static string GetCodstring(string UnName,string Coding) { try { int i = 0; ushort key = 0; string strResult = string.Empty; Encoding unicode = Encoding.Unicode; Encoding gbk = Encoding.GetEncoding(Coding); byte[] unicodeBytes = unicode.GetBytes(UnName); byte[] gbkBytes = Encoding.Convert(unicode, gbk, unicodeBytes); while (i < gbkBytes.Length) { if (gbkBytes[i] <= 127) { strResult = strResult + ((char)gbkBytes[i]).ToString().ToUpper(); i++; } #region 生成汉字拼音简码,取拼音首字母 else { key = (ushort)(gbkBytes[i] * 256 + gbkBytes[i + 1]); if (key >= 'uB0A1' && key <= 'uB0C4') strResult = strResult + "A"; else if (key >= 'uB0C5' && key <= 'uB2C0') strResult = strResult + "B"; else if (key >= 'uB2C1' && key <= 'uB4ED') strResult = strResult + "C"; else if (key >= 'uB4EE' && key <= 'uB6E9') strResult = strResult + "D"; else if (key >= 'uB6EA' && key <= 'uB7A1') strResult = strResult + "E"; else if (key >= 'uB7A2' && key <= 'uB8C0') strResult = strResult + "F"; else if (key >= 'uB8C1' && key <= 'uB9FD') strResult = strResult + "G"; else if (key >= 'uB9FE' && key <= 'uBBF6') strResult = strResult + "H"; else if (key >= 'uBBF7' && key <= 'uBFA5') strResult = strResult + "J"; else if (key >= 'uBFA6' && key <= 'uC0AB') strResult = strResult + "K"; else if (key >= 'uC0AC' && key <= 'uC2E7') strResult = strResult + "L"; else if (key >= 'uC2E8' && key <= 'uC4C2') strResult = strResult + "M"; else if (key >= 'uC4C3' && key <= 'uC5B5') strResult = strResult + "N"; else if (key >= 'uC5B6' && key <= 'uC5BD') strResult = strResult + "O"; else if (key >= 'uC5BE' && key <= 'uC6D9') strResult = strResult + "P"; else if (key >= 'uC6DA' && key <= 'uC8BA') strResult = strResult + "Q"; else if (key >= 'uC8BB' && key <= 'uC8F5') strResult = strResult + "R"; else if (key >= 'uC8F6' && key <= 'uCBF9') strResult = strResult + "S"; else if (key >= 'uCBFA' && key <= 'uCDD9') strResult = strResult + "T"; else if (key >= 'uCDDA' && key <= 'uCEF3') strResult = strResult + "W"; else if (key >= 'uCEF4' && key <= 'uD1B8') strResult = strResult + "X"; else if (key >= 'uD1B9' && key <= 'uD4D0') strResult = strResult + "Y"; else if (key >= 'uD4D1' && key <= 'uD7F9') strResult = strResult + "Z"; else strResult = strResult + "?"; i = i + 2; } #endregion } return strResult; } catch (Exception e) { Console.WriteLine(e.ToString()); return ""; } } /// <summary> /// 在指定的字符串列表CnStr中检索符合拼音索引字符串 /// </summary> /// <param name="CnStr">汉字字符串</param> /// <returns>相对应的汉语拼音首字母串</returns> public static string GetSpellCode(string CnStr) { string strTemp = ""; int iLen = CnStr.Length; int i = 0; for (i = 0; i <= iLen - 1; i++) { strTemp += GetCharSpellCode(CnStr.Substring(i, 1)); } return strTemp; } /// <summary> /// 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母 /// </summary> /// <param name="CnChar">单个汉字</param> /// <returns>单个大写字母</returns> private static string GetCharSpellCode(string CnChar) { long iCnChar; byte[] ZW = System.Text.Encoding.GetEncoding("GB2312").GetBytes(CnChar); //如果是字母,则直接返回 if (ZW.Length == 1) return CnChar.ToUpper(); else { // 从单个字符获取字节数组 int i1 = (short)(ZW[0]); int i2 = (short)(ZW[1]); iCnChar = i1 * 256 + i2; } /* 'A': //45217..45252 // 'B': //45253..45760 // 'C': //45761..46317 // 'D': //46318..46825 // 'E': //46826..47009 // 'F': //47010..47296 // 'G': //47297..47613 // 'H': //47614..48118 // 'J': //48119..49061 // 'K': //49062..49323 // 'L': //49324..49895 // 'M': //49896..50370 // 'N': //50371..50613 // 'O': //50614..50621 // 'P': //50622..50905 // 'Q': //50906..51386 // 'R': //51387..51445 // 'S': //51446..52217 // 'T': //52218..52697 // 汉字没有没有U,V 开始的 // 'W': //52698..52979 // 'X': //52980..53688 // 'Y': //53689..54480 // 'Z': //54481..55289 // iCnChar 匹配常量*/ if ((iCnChar >= 45217) && (iCnChar <= 45252)) return "A"; else if ((iCnChar >= 45253) && (iCnChar <= 45760)) return "B"; else if ((iCnChar >= 45761) && (iCnChar <= 46317)) return "C"; else if ((iCnChar >= 46318) && (iCnChar <= 46825)) return "D"; else if ((iCnChar >= 46826) && (iCnChar <= 47009)) return "E"; else if ((iCnChar >= 47010) && (iCnChar <= 47296)) return "F"; else if ((iCnChar >= 47297) && (iCnChar <= 47613)) return "G"; else if ((iCnChar >= 47614) && (iCnChar <= 48118)) return "H"; else if ((iCnChar >= 48119) && (iCnChar <= 49061)) return "J"; else if ((iCnChar >= 49062) && (iCnChar <= 49323)) return "K"; else if ((iCnChar >= 49324) && (iCnChar <= 49895)) return "L"; else if ((iCnChar >= 49896) && (iCnChar <= 50370)) return "M"; else if ((iCnChar >= 50371) && (iCnChar <= 50613)) return "N"; else if ((iCnChar >= 50614) && (iCnChar <= 50621)) return "O"; else if ((iCnChar >= 50622) && (iCnChar <= 50905)) return "P"; else if ((iCnChar >= 50906) && (iCnChar <= 51386)) return "Q"; else if ((iCnChar >= 51387) && (iCnChar <= 51445)) return "R"; else if ((iCnChar >= 51446) && (iCnChar <= 52217)) return "S"; else if ((iCnChar >= 52218) && (iCnChar <= 52697)) return "T"; else if ((iCnChar >= 52698) && (iCnChar <= 52979)) return "W"; else if ((iCnChar >= 52980) && (iCnChar <= 53688)) return "X"; else if ((iCnChar >= 53689) && (iCnChar <= 54480)) return "Y"; else if ((iCnChar >= 54481) && (iCnChar <= 55289)) return "Z"; else return ("?"); }