程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> c#漢字轉化成拼音的源代碼

c#漢字轉化成拼音的源代碼

編輯:關於C#
 

1using System;
2using System.Collections;
3using System.Text;
4namespace JJBase.String
5{
6 /**//// <summary>
7 /// JJBase 的摘要說明。
8 /// </summary>
9 ///
10 漢字轉化成拼音#region 漢字轉化成拼音
11
12 public class Chinese2Spell
13 {
14
15 /**//// <summary>
16 /// 獲得拼音
17 /// </summary>
18 /// <param name="str_Spell">漢字</param>
19 /// <returns></returns>
20 //調用格式:GetSpell(string str_Spell)
21 //str_Spell是要轉換的漢字
22 //返回結果是轉化成的拼音
23 //目前還不完善,例如“爨”字轉換不出來,需要進一步完善
24 public string GetSpell(string str_Chinese)
25 {
26
27 try
28 {
29 Hashtable t=hb();
30
31 byte[] b=System.Text.Encoding.Default.GetBytes(str_Chinese);
32 int p;
33 StringBuilder ret=new StringBuilder();
34 for(int i=0;i< b.Length;i++)
35 {
36 p=(int)b[i];
37 if(p>128)//160? or 128? 原文是160,待以後考證
38 {
39 p=p*256+b[++i]-65536;
40 ret.Append(g(t,p));
41 }
42 else
43 {
44 ret.Append((char)p);
45 }
46 }
47 t.Clear();
48 return ret.ToString();
49 }
50 catch
51 {
52 return "";
53 }
54
55 }
56
57 private string g(Hashtable ht,int num)
58 {
59 if(num < -20319||num > -10247)
60 return "";
61 while(!ht.ContainsKey(num))
62 num--;
63 return ht[num].ToString();
64 }
65 private Hashtable hb()
66 {
67 //尚不晚完善,例如“爨”就沒有轉換出來
68 Hashtable ht=new Hashtable();
69 ht.Add(-20319,"a");
70 ht.Add(-20317,"ai");ht.Add(-20304,"an"); ht.Add(-20295,"ang");
71 ht.Add(-20292,"ao");ht.Add(-20283,"ba"); ht.Add(-20265,"bai");
72 ht.Add(-20257,"ban");ht.Add(-20242,"bang"); ht.Add(-20230,"bao");
73 ht.Add(-20051,"bei"); ht.Add(-20036,"ben"); ht.Add(-20032,"beng");
74 ht.Add(-20026,"bi"); ht.Add(-20002,"bian"); ht.Add(-19990,"biao");
75 ht.Add(-19986,"bie"); ht.Add(-19982,"bin"); ht.Add(-19976,"bing");
76 ht.Add(-19805,"bo"); ht.Add(-19784,"bu"); ht.Add(-19775,"ca");
77 ht.Add(-19774,"cai"); ht.Add(-19763,"can"); ht.Add(-19756,"cang");
78 ht.Add(-19751,"cao"); ht.Add(-19746,"ce"); ht.Add(-19741,"ceng");
79 ht.Add(-19739,"cha"); ht.Add(-19728,"chai"); ht.Add(-19725,"chan");
80 ht.Add(-19715,"chang"); ht.Add(-19540,"chao"); ht.Add(-19531,"che");
81 ht.Add(-19525,"chen"); ht.Add(-19515,"cheng"); ht.Add(-19500,"chi");
82 ht.Add(-19484,"chong"); ht.Add(-19479,"chou"); ht.Add(-19467,"chu");
83 ht.Add(-19289,"chuai"); ht.Add(-19288,"chuan"); ht.Add(-19281,"chuang");
84 ht.Add(-19275,"chui"); ht.Add(-19270,"chun"); ht.Add(-19263,"chuo");
85 ht.Add(-19261,"ci"); ht.Add(-19249,"cong"); ht.Add(-19243,"cou");
86 ht.Add(-19242,"cu"); ht.Add(-19238,"cuan"); ht.Add(-19235,"cui");
87 ht.Add(-19227,"cun"); ht.Add(-19224,"cuo"); ht.Add(-19218,"da");
88 ht.Add(-19212,"dai"); ht.Add(-19038,"dan"); ht.Add(-19023,"dang");
89 ht.Add(-19018,"dao"); ht.Add(-19006,"de"); ht.Add(-19003,"deng");
90 ht.Add(-18996,"di"); ht.Add(-18977,"dian"); ht.Add(-18961,"diao");
91 ht.Add(-18952,"die"); ht.Add(-18783,"ding"); ht.Add(-18774,"diu");
92 ht.Add(-18773,"dong"); ht.Add(-18763,"dou"); ht.Add(-18756,"du");
93 ht.Add(-18741,"duan"); ht.Add(-18735,"dui"); ht.Add(-18731,"dun");
94 ht.Add(-18722,"duo"); ht.Add(-18710,"e"); ht.Add(-18697,"en");
95 ht.Add(-18696,"er"); ht.Add(-18526,"fa"); ht.Add(-18518,"fan");
96 ht.Add(-18501,"fang"); ht.Add(-18490,"fei"); ht.Add(-18478,"fen");
97 ht.Add(-18463,"feng"); ht.Add(-18448,"fo"); ht.Add(-18447,"fou");
98 ht.Add(-18446,"fu"); ht.Add(-18239,"ga"); ht.Add(-18237,"gai");
99 ht.Add(-18231,"gan"); ht.Add(-18220,"gang"); ht.Add(-18211,"gao");

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved