C#汉字转拼音
下面这个方法里 ' _Allhz' 是在什么地方声明的?
还有一个地方报错,因为给删了,想不起来了;
大家有用过这个方法的?
public static string ConvertPY(string SourceString)
{
if (SourceString == null)
return null;
Encoding ed = Encoding.GetEncoding("GB2312");
if (ed == null)
throw (new ArgumentException("没有找到编码集GB2312"));
int bh = 0;
char[] charary = SourceString.ToCharArray();
byte[] bAry = new byte[2];
StringBuilder rtnSb = new StringBuilder();
for (int i = 0; i < charary.Length; i++)
{
bAry = ed.GetBytes(charary[i].ToString());
if (bAry.Length == 1)
{
rtnSb.Append(charary[i]);
continue;
}
bh = bAry[0] - 0xA0;
if (0x10 <= bh && bh <= 0x57) //是gb2312汉字
{
bool isFind = false;
for (int j = 0; j < _Allhz.Length; j++)
{
if (_Allhz[j][1].IndexOf(charary[i]) != -1)
{
rtnSb.Append(_Allhz[j][0]);
isFind = true;
break;
}
}
if (!isFind)
rtnSb.Append(charary[i]);
}
else
rtnSb.Append(charary[i]);
}
return rtnSb.ToString();
}
[解决办法]
给你一段我自己用过的吧 net2003的代码 2005下也好用
//获取简体中文字符串拼音首字母
static public string getSpells(string input)
{
int len = input.Length;
string reVal = "";
for(int i=0;i<len;i++)
{
reVal += getSpell(input.Substring(i,1));
}
return reVal;
}
//获取一个简体中文字的拼音首字母
static public string getSpell(string cn)
{
byte[] arrCN = System.Text.Encoding.Default.GetBytes(cn);
if(arrCN.Length > 1)
{
int area = (short)arrCN[0];
int pos = (short)arrCN[1];
int code = (area<<8) + pos;
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};
for(int i=0;i<26;i++)
{
int max = 55290;
if(i != 25) max = areacode[i+1];
if(areacode[i]<=code && code<max)
{
return System.Text.Encoding.Default.GetString(new byte[]{(byte)(65+i)});
}
}
return "?";
}
else return cn;
}
[解决办法]
/// <summary>
/// 汉字转拼音缩写
///<param name="str">要转换的汉字字符串</param>
///<returns>拼音缩写</returns>
public string GetPYString(string str)
{
string tempStr = "";
foreach(char c in str)
{
//if((int)c >= 33 && (int)c <=126) //2007/02/14 修改
if((int)c >= 33 && (int)c <=126 || (int) c == 32 || (int) c == 65289 || (int) c == 65288) //(int) c == 65289 || (int) c == 65288 当输入全角括号时
{//字母和符号原样保留
if((int) c == 65289 || (int) c == 65288)
{
if((int) c == 65288) tempStr += "(";//输入全角括号时变为半角
if((int) c == 65289) tempStr += ")";
}
else
tempStr += c.ToString();
}
else
{//累加拼音声母
tempStr += GetPYChar(c.ToString());
}
}
return tempStr;
}
/// <summary>
/// 取单个字符的拼音声母
/// <param name="c">要转换的单个汉字</param>
/// <returns>拼音声母</returns>
public string GetPYChar(string c)
{
byte[] array = new byte[2];
array = System.Text.Encoding.Default.GetBytes(c);
int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
if(i == 58557) return "H";//浣字的首字母
if(i == 63182) return "X";//鑫字首字母
if(i == 60105) return "S";//晟字首字母
if(i == 59627) return "H";//桦字首字母
if ( i < 0xB0A1) return "*";
if ( i < 0xB0C5) return "A";
if ( i < 0xB2C1) return "B";
if ( i < 0xB4EE) return "C";
if ( i < 0xB6EA) return "D";
if ( i < 0xB7A2) return "E";
if ( i < 0xB8C1) return "F";
if ( i < 0xB9FE) return "G";
if ( i < 0xBBF7) return "H";
if ( i < 0xBFA6) return "J";
if ( i < 0xC0AC) return "K";
if ( i < 0xC2E8) return "L";
if ( i < 0xC4C3) return "M";
if ( i < 0xC5B6) return "N";
if ( i < 0xC5BE) return "O";
if ( i < 0xC6DA) return "P";
if ( i < 0xC8BB) return "Q";
if ( i < 0xC8F6) return "R";
if ( i < 0xCBFA) return "S";
if ( i < 0xCDDA) return "T";
if ( i < 0xCEF4) return "W";
if ( i < 0xD1B9) return "X";
if ( i < 0xD4D1) return "Y";
if ( i < 0xD7FA) return "Z";
return "*";
}
大部分汉字都好用,例如 中国 转成 zg