C # Chinese character conversion Pinyin technology detailed (high-performance) _c# Tutorial

Source: Internet
Author: User
Tags static class
The source code is posted below.

Copy Code code as follows:

public static Class Chinesetopinyin
{
private static readonly Dictionary<int, string> codecollections = new Dictionary<int, string> {
{ -20319, "a"}, { -20317, "AI"}, { -20304, "an"}, { -20295, "Ang"}, { -20292, "AO"}, { -20283, "ba"}, { -20265, "Bai" " },
{ -20257, "ban"}, { -20242, "bang"}, { -20230, "Bao"}, { -20051, "bei"}, { -20036, "Ben"}, { -20032, "Beng"}, {-200 num, "BI"}
, { -20002, "Bian"}, { -19990, "Biao"}, { -19986, "Bie"}, { -19982, "bin"}, { -19976, "Bing"}, { -19805, "bo"},
{ -19784, "bu"}, { -19775, "Ca"}, { -19774, "Cai"}, { -19763, "can"}, { -19756, "Cang"}, { -19751, "Cao"}, {-19746, "Ce"},
{ -19741, "CEng"}, { -19739, "Cha"}, { -19728, "chai"}, { -19725, "Chan"}, { -19715, "chang"}, { -19540, "Chao"},
{ -19531, "Che"}, { -19525, "Chen"}, { -19515, "Cheng"}, { -19500, "Chi"}, { -19484, "Chong"}, { -19479, "Chou"},
{ -19467, "Chu"}, { -19289, "Chuai"}, { -19288, "Chuan"}, { -19281, "Chuang"}, { -19275, "Chui"}, { -19270, "Chun"},
{ -19263, "Chuo"}, { -19261, "ci"}, { -19249, "cong"}, { -19243, "cou"}, { -19242, "cu"}, { -19238, "Cuan"},
{ -19235, "Cui"}, { -19227, "Cun"}, { -19224, "CuO"}, { -19218, "Da"}, { -19212, "Dai"}, { -19038, "Dan"}, {-19023, "Dang"},
{ -19018, "DAO"}, { -19006, "de"}, { -19003, "Deng"}, { -18996, "Di"}, { -18977, "Dian"}, { -18961, "Diao"}, {-1895 2, "Die"}
, { -18783, "ding"}, { -18774, "Diu"}, { -18773, "dong"}, { -18763, "dou"}, { -18756, "du"}, { -18741, "Duan"},
{ -18735, "DUI"}, { -18731, "dun"}, { -18722, "Duo"}, { -18710, "E"}, { -18697, "en"}, { -18696, "er"}, { -18526, "f A "},
{ -18518, "fan"}, { -18501, "Fang"}, { -18490, "Fei"}, { -18478, "fen"}, { -18463, "Feng"}, { -18448, "fo"}, {-1844 7, "Fou"}
, { -18446, "fu"}, { -18239, "ga"}, { -18237, "Gai"}, { -18231, "Gan"}, { -18220, "gang"}, { -18211, "Gao"}, {-1820 1, "GE"},
{ -18184, "GEi"}, { -18183, "gen"}, { -18181, "Geng"}, { -18012, "Gong"}, { -17997, "Gou"}, { -17988, "gu"}, {-1797 0, "Gua"}
, { -17964, "Guai"}, { -17961, "Guan"}, { -17950, "Guang"}, { -17947, "GUI"}, { -17931, "gun"}, { -17928, "Guo"},
{ -17922, "ha"}, { -17759, "Hai"}, { -17752, "Han"}, { -17733, "hang"}, { -17730, "Hao"}, { -17721, "he"}, {-17703, "Hei"},
{ -17701, "hen"}, { -17697, "Heng"}, { -17692, "$"}, { -17683, "hou"}, { -17676, "Hu"}, { -17496, "Hua"},
{ -17487, "Huai"}, { -17482, "Huan"}, { -17468, "Huang"}, { -17454, "hui"}, { -17433, "Hun"}, { -17427, "Huo"},
{ -17417, "Ji"}, { -17202, "Jia"}, { -17185, "jian"}, { -16983, "jiang"}, { -16970, "jiao"}, { -16942, "Jie"},
{ -16915, "Jin"}, { -16733, "Jing"}, { -16708, "Jiong"}, { -16706, "JIU"}, { -16689, "Ju"}, { -16664, "Juan"},
{ -16657, "Jue"}, { -16647, "June"}, { -16474, "ka"}, { -16470, "Kai"}, { -16465, "Kan"}, { -16459, "Kang"}, {-16452 , "Kao"},
{ -16448, "Ke"}, { -16433, "Ken"}, { -16429, "Keng"}, { -16427, "Kong"}, { -16423, "Kou"}, { -16419, "Ku"}, {-16412 , "Kua"}
, { -16407, "Kuai"}, { -16403, "Kuan"}, { -16401, "Kuang"}, { -16393, "Kui"}, { -16220, "Kun"}, { -16216, "Kuo"},
{ -16212, "la"}, { -16205, "Lai"}, { -16202, "LAN"}, { -16187, "lang"}, { -16180, "Lao"}, { -16171, "le"}, {-16169, "Lei"},
{ -16158, "Leng"}, { -16155, "Li"}, { -15959, "lia"}, { -15958, "Lian"}, { -15944, "Liang"}, { -15933, "Liao"},
{ -15920, "lie"}, { -15915, "Lin"}, { -15903, "ling"}, { -15889, "Liu"}, { -15878, "long"}, { -15707, "Lou"}, {-157 "Lu"},
{ -15681, "LV"}, { -15667, "Luan"}, { -15661, "Lue"}, { -15659, "lun"}, { -15652, "Luo"}, { -15640, "Ma"}, {-15631, "Mai"},
{ -15625, "man"}, { -15454, "Mang"}, { -15448, "Mao"}, { -15436, "Me"}, { -15435, "Mei"}, { -15419, "men"},
{ -15416, "Meng"}, { -15408, "Mi"}, { -15394, "Mian"}, { -15385, "Miao"}, { -15377, "mie"}, { -15375, "min"},
{ -15369, "Ming"}, { -15363, "Miu"}, { -15362, "Mo"}, { -15183, "MoU"}, { -15180, "mu"}, { -15165, "NA"}, {-15158, "Nai"},
{ -15153, "Nan"}, { -15150, "Nang"}, { -15149, "Nao"}, { -15144, "ne"}, { -15143, "nei"}, { -15141, "nen"}, {-15140 , "Neng"}
, { -15139, "ni"}, { -15128, "Nian"}, { -15121, "Niang"}, { -15119, "Niao"}, { -15117, "nie"}, { -15110, "nin"},
{ -15109, "ning"}, { -14941, "niu"}, { -14937, "Nong"}, { -14933, "Nu"}, { -14930, "NV"}, { -14929, "Nuan"}, {-1492 8, "Nue"}
, { -14926, "Nuo"}, { -14922, "O"}, { -14921, "ou"}, { -14914, "Pa"}, { -14908, "pai"}, { -14902, "Pan"}, {-14894, "Pang"},
{ -14889, "pao"}, { -14882, "Pei"}, { -14873, "pen"}, { -14871, "peng"}, { -14857, "PI"}, { -14678, "Pian"},
{ -14674, "Piao"}, { -14670, "pie"}, { -14668, "pin"}, { -14663, "ping"}, { -14654, "po"}, { -14645, "Pu"}, {-14630 , "Qi"},
{ -14594, "QIA"}, { -14429, "Qian"}, { -14407, "Qiang"}, { -14399, "Qiao"}, { -14384, "Qie"}, { -14379, "Qin"},
{ -14368, "Qing"}, { -14355, "Qiong"}, { -14353, "Qiu"}, { -14345, "qu"}, { -14170, "Quan"}, { -14159, "que"},
{ -14151, "Qun"}, { -14149, "ran"}, { -14145, "rang"}, { -14140, "Rao"}, { -14137, "re"}, { -14135, "ren"}, {-14125 , "Reng"}
, { -14123, "ri"}, { -14122, "Rong"}, { -14112, "Rou"}, { -14109, "ru"}, { -14099, "Ruan"}, { -14097, "Rui"}, {-140 , "Run"}
, { -14092, "Ruo"}, { -14090, "sa"}, { -14087, "sai"}, { -14083, "san"}, { -13917, "sang"}, { -13914, "Sao"}, {-139 Ten, "Se"}
, { -13907, "sen"}, { -13906, "Seng"}, { -13905, "Sha"}, { -13896, "Shai"}, { -13894, "Shan"}, { -13878, "Shang"},
{ -13870, "Shao"}, { -13859, "she"}, { -13847, "shen"}, { -13831, "Sheng"}, { -13658, "Shi"}, { -13611, "Shou"},
{ -13601, "Shu"}, { -13406, "Shua"}, { -13404, "Shuai"}, { -13400, "Shuan"}, { -13398, "Shuang"}, { -13395, "shui"},
{ -13391, "shun"}, { -13387, "Shuo"}, { -13383, "si"}, { -13367, "song"}, { -13359, "sou"}, { -13356, "su"},
{ -13343, "Suan"}, { -13340, "sui"}, { -13329, "sun"}, { -13326, "suo"}, { -13318, "ta"}, { -13147, "Tai"}, {-13138 , "Tan"},
{ -13120, "tang"}, { -13107, "Tao"}, { -13096, "TE"}, { -13095, "teng"}, { -13091, "Ti"}, { -13076, "Tian"},
{ -13068, "Tiao"}, { -13063, "tie"}, { -13060, "ting"}, { -12888, "tong"}, { -12875, "tou"}, { -12871, "tu"},
{ -12860, "Tuan"}, { -12858, "Tui"}, { -12852, "tun"}, { -12849, "Tuo"}, { -12838, "WA"}, { -12831, "Wai"}, {-12829 , "Wan"}
, { -12812, "Wang"}, { -12802, "Wei"}, { -12607, "Wen"}, { -12597, "Weng"}, { -12594, "Wo"}, { -12585, "WU"}, {-125 , "XI"}
, { -12359, "Xia"}, { -12346, "Xian"}, { -12320, "Xiang"}, { -12300, "Xiao"}, { -12120, "Xie"}, { -12099, "xin"},
{ -12089, "xing"}, { -12074, "Xiong"}, { -12067, "Xiu"}, { -12058, "Xu"}, { -12039, "Xuan"}, { -11867, "Xue"},
{ -11861, "Xun"}, { -11847, "Ya"}, { -11831, "Yan"}, { -11798, "yang"}, { -11781, "Yao"}, { -11604, "Ye"}, {-11589, "Yi"},
{ -11536, "yin"}, { -11358, "ying"}, { -11340, "Yo"}, { -11339, "Yong"}, { -11324, "you"}, { -11303, "Yu"},
{ -11097, "Yuan"}, { -11077, "Yue"}, { -11067, "Yun"}, { -11055, "Za"}, { -11052, "Zai"}, { -11045, "Zan"},
{ -11041, "Zang"}, { -11038, "Zao"}, { -11024, "ze"}, { -11020, "Zei"}, { -11019, "Zen"}, { -11018, "Zeng"},
{ -11014, "Zha"}, { -10838, "Zhai"}, { -10832, "Zhan"}, { -10815, "Zhang"}, { -10800, "Zhao"}, { -10790, "Zhe"},
{ -10780, "zhen"}, { -10764, "Zheng"}, { -10587, "zhi"}, { -10544, "Zhong"}, { -10533, "Zhou"}, { -10519, "Zhu"},
{ -10331, "Zhua"}, { -10329, "Zhuai"}, { -10328, "Zhuan"}, { -10322, "Zhuang"}, { -10315, "Zhui"}, { -10309, "Zhun"} ,
{ -10307, "Zhuo"}, { -10296, "Zi"}, { -10281, "zong"}, { -10274, "Zou"}, { -10270, "zu"}, { -10262, "Zuan"}, {-1026 0, "Zui"}
, { -10256, "Zun"}, { -10254, "Zuo"}};
<summary>
Pinyin for Chinese character conversion
</summary>
<param name= "txt" > need to convert Chinese characters </param>
<returns> return Chinese Pinyin </returns>
public static string Topinyin (String txt)
{
txt = txt. Trim ();
byte[] arr = new byte[2]; Each Chinese character is 2 bytes
StringBuilder result = new StringBuilder ();//Use StringBuilder to optimize string concatenation
int charcode = 0;
int arr1 = 0;
int arr2 = 0;
char[] Arrchar = txt. ToCharArray ();
for (int j = 0; J < Arrchar.length; J + +)//Traverse entered characters
{
arr = System.Text.Encoding.Default.GetBytes (Arrchar[j]. ToString ())//byte code based on system default encoding
if (arr. Length = = 1)//If only 1 bytes indicate that the character is not Chinese, end this loop
{
Result. Append (Arrchar[j]. ToString ());
Continue

}
arr1 = (short) (arr[0]); Fetch bytes 1
ARR2 = (short) (arr[1]); Fetch bytes 2
CharCode = arr1 * 256 + arr2-65536;//Computing The encoding of Chinese characters

if (CharCode > -10254 | | charcode <-20319)////If not within encoding range, no change
{
Result. Append (Arrchar[j]);
}
Else
{
Find the corresponding Ruby according to the encoding range and save it to the result
Because the CharCode key does not necessarily exist, you should find a key that is smaller than his.
if (! Codecollections.containskey (CharCode))
{
for (int i = CharCode i <= 0; i.)
{
if (Codecollections.containskey (i))
{
Result. Append ("" + Codecollections[i] + "");
Break
}
}
}
Else
{
Result. Append ("" + Codecollections[charcode] + "");
}
}
}
return result. ToString ();
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.