C#汉字转拼音

crecrecre 2009-02-19 12:45:41
下面这个方法里 ' _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();
}

...全文
5446 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
adandelion 2011-07-25
  • 打赏
  • 举报
回复
mark yi xia
Coder归来 2009-12-03
  • 打赏
  • 举报
回复
拷贝学习了
oneatree 2009-10-31
  • 打赏
  • 举报
回复
MARK
huang_u 2009-10-11
  • 打赏
  • 举报
回复
Mark
xcStone 2009-05-19
  • 打赏
  • 举报
回复
感觉如果只是要拼音的话,很多方式都可以,如果是词组的拼音,不知各位英雄,哪里有好的解决方式?
例如:济南,救济中的“济”字,因为在不同的词组中拼音是不同的,如何处理,还请各位英雄支个招,这厢谢谢先!
hannjian 2009-02-20
  • 打赏
  • 举报
回复
/// <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
zgke 2009-02-19
  • 打赏
  • 举报
回复
恩 我是用字典表来转换的
  • 打赏
  • 举报
回复
网上的汉字转拼音 都没有完全做到
很多字符 都是转换不出来的
huanxiangsugars 2009-02-19
  • 打赏
  • 举报
回复
我这边有sql汉字转换成首字母的代码
CREATE function f_GetFirstPy(@str varchar(50)='')
returns char(1)
as
begin
declare @n int, @c char(1), @chn nchar(1)
select @n=63, @chn=substring(@str,1,1)
if @chn > 'z'
if( @chn < '八' ) set @c = 'A'
else if ( @chn < '嚓' ) set @c = 'B'
else if ( @chn < '咑' ) set @c = 'C'
else if ( @chn < '妸' ) set @c = 'D'
else if ( @chn < '发' ) set @c = 'E'
else if ( @chn < '旮' ) set @c = 'F'
else if ( @chn < '铪' ) set @c = 'G'
else if ( @chn < '丌' ) set @c = 'H'
else if ( @chn < '丌' ) set @c = 'I'
else if ( @chn < '咔' ) set @c = 'J'
else if ( @chn < '垃' ) set @c = 'K'
else if ( @chn < '嘸' ) set @c = 'L'
else if ( @chn < '拏' ) set @c = 'M'
else if ( @chn < '噢' ) set @c = 'N'
else if ( @chn < '妑' ) set @c = 'O'
else if ( @chn < '七' ) set @c = 'P'
else if ( @chn < '呥' ) set @c = 'Q'
else if ( @chn < '仨' ) set @c = 'R'
else if ( @chn < '他' ) set @c = 'S'
else if ( @chn < '屲' ) set @c = 'T'
else if ( @chn < '屲' ) set @c = 'U'
else if ( @chn < '屲' ) set @c = 'V'
else if ( @chn < '夕' ) set @c = 'W'
else if ( @chn < '丫' ) set @c = 'X'
else if ( @chn < '帀' ) set @c = 'Y'
else set @c = 'Z'
else if @chn <= 'z' and @chn >= 'a'
set @c = upper(@chn)
else if @chn <= 'Z' and @chn >= 'A'
set @c = @chn
else
set @c =@chn
return (@c)
end
差不多,把它转换成c#就好了。
MikeCheers 2009-02-19
  • 打赏
  • 举报
回复
标记
baichangle 2009-02-19
  • 打赏
  • 举报
回复
留个脚印~
MikeCheers 2009-02-19
  • 打赏
  • 举报
回复
Mark
DMU_WD 2009-02-19
  • 打赏
  • 举报
回复
就是mark太少了!呵呵
DMU_WD 2009-02-19
  • 打赏
  • 举报
回复
给你一段我自己用过的吧 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;
}
crecrecre 2009-02-19
  • 打赏
  • 举报
回复
[Quote]
...全部的拼音你都有了,自己取下第一个字符不久可以了么...
[/Quote]

那不是麻烦?
xiyun 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 crecrecre 的回复:]
微软的是全部拼音;
我想要的是,比如:中国 ZG ;这种形式
[/Quote]
...全部的拼音你都有了,自己取下第一个字符不久可以了么...
xiyun 2009-02-19
  • 打赏
  • 举报
回复
2008年4月初,微软发布Visual Studio International Pack 1.0版。这是一组类库,扩展了.NET Framework对国际化软件开发的支持。

使用该类库提供的一系列类,.NET开发人员可以更方便地创建支持多文化多语言的软件应用。目前,该软件包1.0版提供了如下七个组件(包含简体中文、繁体中文、日文和韩文的支持)来帮助东亚国家及地区的开发人员,用于开发.NET国际化应用程序:

◆East Asia Numeric Formatting Library - 支持将小写的数字字符串格式化成简体中文,繁体中文,日文和韩文的大写数字字符串。
◆Japanese Kana Conversion Library - 支持将日文假名(Kana)转化为另一种日文字符。
◆Japanese Text Alignment Library - 支持日文特有的一种对齐格式。
◆Japanese Yomi Auto-Completion Library - 类库支持感知日文输入法的输入自动完成和一个文本框控制的示例。
◆Korean Auto Complete TextBox Control - 在文本框中支持韩文输入法的智能感知和输入自动完成。
◆Simplified Chinese Pin-Yin Conversion Library - 支持获取简体中文字符的常用属性比如拼音,多音字,同音字,笔画数。
◆Traditional Chinese to Simplified Chinese Conversion Library and Add-In Tool - 支持简繁体中文之间的转换. 该组件还包含一个Visual Studio集成开发环境中的插件(Add-in)以支持简繁体中文资源文件之间的转换。

这个软件包的出现,对于进行本地化开发的开发人员来说,最大的好处是无需另外发明一个轮子。对于中文用户,现在有了East Asia Numeric Formatting Library、Simplified Chinese Pin-Yin Conversion Library和Traditional Chinese to Simplified Chinese Conversion Library and Add-In Tool,可以在自己的应用程序中可以很方便地加入一些中文字符的处理。比如,在金融和财务类的软件中,可以使用East Asia Numeric Formatting Library来进行货币数值的大小写转换。

关于这个软件包的一些使用方法,博客园的一位作者有一些介绍:

//汉字的笔画数
int i = ChineseChar.GetStrokeNumber('饕');
Console.WriteLine(i);


//汉字的所有拼音拼写
ChineseChar x = new ChineseChar('说');
ReadOnlyCollection roc = x.Pinyins;
foreach(string s in roc)
{
Console.WriteLine(s);
}


//拼音拼写的所有汉字
Char[] chars = ChineseChar.GetChars("YUE4");
foreach(Char c in chars)
{
Console.WriteLine(c);
}


//笔画数的所有汉字
chars = ChineseChar.GetChars(20);
foreach(Char c in chars)
{
Console.WriteLine(c);
}


当然,这个软件包中也存在一些问题。Traditional Chinese to Simplified Chinese Conversion Library and Add-In Tool依赖于Office 2007来实现词对词的高质量转换;在没有安装Office 2007的情况下,只能实现简单的字对字的转换。同时,这个软件包的程序集在Windows Mobile和Silverlight项目中都不能正常使用。想在PPC上实现这样一个应用程序——手写输入一个不知道读音的汉字,来获知这个汉字的拼音——现在看来是暂时不可能了。
crecrecre 2009-02-19
  • 打赏
  • 举报
回复
微软的是全部拼音;
我想要的是,比如:中国 ZG ;这种形式
virginboa 2009-02-19
  • 打赏
  • 举报
回复
不懂!~纯帮顶~
crecrecre 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xiyun 的回复:]
这个微软自己就出了一个扩展包解决了这个问题的。微软的网站上搜下看看吧
[/Quote]

下载了,不好用!
加载更多回复(3)

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧