- 话不多说,直接上代码,代码参考网上源码.其中存在X显示为?的问题.一共两种写法.目前该写法还是存在部分汉字无法正常转化的问题.另外一种方法在另一个帖子中说明.
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 "";
}
}
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;
}
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;
}
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 ("?");
}
本文作者:Joe_du
本文链接:https://www.cnblogs.com/dygood/p/7595519.html
版权声明:本作品采用 MIT License 许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步