Valid only for GB2312 Encoding
/* Array of the code values at the Chinese pronunciation demarcation points */
Static const unsigned short code_pin [] = {
0xb0a1, 0xb0a3, expires, 0xb0b9, 0xb0bc, 0xb0c5, 0xb0d7, 0xb0df, 0xb0ee, 0xb0fa, 0xb1ad, 0xb1bc, 0xb1c0, 0xb1c6,
0xb1de, 0xb1ea, 0xb1ee, 0xb1f2, 0xb1f8, 0xb2a3, 0xb2b8, 0xb2c1, 0xb2c2, 0xb2cd, 0xb2d4, 0xb2d9, 0xb2de, 0xb2e3,
0xb2e5, 0xb2f0, 0xb2f3, 0xb2fd, 0xb3ac, 0xb3b5, 0xb3bb, 0xb3c5, 0xb3d4, 0xb3e4, 0xb3e9, 0xb3f5, 0xb4a7, 0xb4a8,
0xb4af, 0xb4b5, 0xb4ba, 0xb4c1, 0xb4c3, 0xb4cf, 0xb4d5, 0xb4d6, 0xb4da, 0xb4dd, primary, 0xb4e8, 0xb4ee, 0xb4f4,
0xb5a2, 0xb5b1, 0xb5b6, 0xb5c2, 0xb5c5, 0xb5cc, 0xb5df, 0xb5ef, 0xb5f8, 0xb6a1, 0xb6aa, 0xb6ab, 0xb6b5, 0xb6bc,
0xb6cb, 0xb6d1, 0xb6d5, 0xb6de, 0xb6ea, 0xb6f7, 0xb6f8, 0xb7a2, 0xb7aa, 0xb7bb, 0xb7c6, 0xb7d2, 0xb7e1, 0xb7f0,
0xb7f1, 0xb7f2, 0xb8c1, 0xb8c3, 0xb8c9, 0xb8d4, 0xb8dd, 0xb8e7, 0xb8f8, 0xb8f9, 0xb8fb, 0xb9a4, 0xb9b3, 0xb9bc,
0xb9ce, 0xb9d4, 0xb9d7, 0xb9e2, 0xb9e5, 0xb9f5, 0xb9f8, 0xb9fe, 0xbaa1, 0xbaa8, 0 xbabb, 0 xbabe, 0xbac7, 0xbad9,
0 xbadb, 0 xbadf, 0xbae4, 0 xbaed, 0xbaf4, 0xbba8, 0xbbb1, 0xbbb6, 0xbbc4, 0xbbd2, 0xbbe7, 0 x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d7ae, 0xd7b5, 0xd7bb, 0xd7bd, 0xd7c8, 0xd7d7, 0xd7de, 0xd7e2,
0xd7ea, 0xd7ec, 0xd7f0, 0xd7f2 };
/* Chinese Character pronunciation array */
Static const char * str_pin [] = {
"A", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao ", "bei", "ben", "beng", "bi", "bian", "biao ",
"Bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao ", "ce", "ceng", "cha", "chai", "chan ",
"Chang", "chao", "che", "chen", "cheng", "chi", "chong", "chou", "chu", "chuai ", "chuan", "chuang", "chui ",
"Chun", "chuo", "ci", "cong", "cou", "cu", "cuan", "cui", "cun", "cuo ", "da", "dai", "dan", "dang", "dao ",
"De", "deng", "di", "dian", "diao", "die", "ding", "diu", "dong", "dou ", "du", "duan", "dui", "dun", "duo ",
"E", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo ", "fou", "fu", "ga", "gai", "gan", "gang ",
"Gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai ", "guan", "guang", "gui", "gun ",
"Guo", "ha", "hai", "han", "hang", "hao", "he", "hei", "Hu", "heng ", "hong", "hou", "hu", "hua", "huai ",
"Huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian", "jiang", "jiao ", "jie", "jin", "jing ",
"Jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang ", "kao", "ke", "ken", "keng ",
"Kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo ", "la", "lai", "lan", "lang ",
"Lao", "le", "lei", "leng", "li", "lia", "lian", "liang", "liao", "lie ", "lin", "ling", "liu", "long", "lou ",
"Lu", "lv", "luan", "lue", "lun", "luo", "ma", "mai", "man", "mang ", "mao", "me", "mei", "men", "meng ",
"Mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu ", "na", "nai", "nan", "nang", "nao", "ne ",
"Nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning ", "niu", "nong", "nu", "nv", "nuan ",
"Nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei ", "pen", "peng", "pi", "pian", "piao", "pie ",
"Pin", "ping", "po", "pu", "qi", "qia", "qian", "qiang", "qiao", "qie ", "qin", "qing", "qiong", "qiu", "qu ",
"Quan", "que", "qun", "ran", "rang", "rao", "re", "ren", "reng", "ri ", "rong", "rou", "ru", "ruan", "rui ",
"Run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", "seng ", "sha", "shai", "shan", "shang", "shao ",
"She", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang ", "shui", "shun", "shuo ",
"Si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai ", "tan", "tang", "tao", "te", "teng ",
"Ti", "tian", "tiao", "tie", "ting", "tong", "tou", "tu", "tuan", "tui ", "tun", "tuo", "wa", "wai", "wan ",
"Wang", "wei", "wen", "weng", "wo", "wu", "xi", "xia", "xian", "xiang ", "xiao", "xie", "xin", "xing ",
"Xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao ", "ye", "yi", "yin", "ying", "yo ",
"Yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang ", "zao", "ze", "zei", "zen", "zeng ",
"Zha", "zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong ", "zhou", "zhu", "zhua ",
"Zhuai", "zhuan", "zhuang", "zhui", "zhun", "zhuo", "zi", "zong", "zou", "zu ", "zuan", "zui", "zun", "zuo "};
/* Array length */
Static const size_t SIZE_ARRAY = sizeof (code_pin)/sizeof (short );
# Include <iostream>
# Include <string>
Using std: string;
Using std: cout;
Using std: endl;
/* Obtain the Pinyin of a Chinese character */
Const char * get_pin (unsigned short char_zh)
{
Size_t low = 0, high = SIZE_ARRAY-1;
Size_t index;
While (high-low! = 1)
{
Index = (low + high)/2;
If (code_pin [index] = char_zh) return str_pin [index];
If (code_pin [index] <char_zh) low = index;
Else high = index;
}
Return str_pin [code_pin [high] <= char_zh? High: low];
}
/* Convert a Chinese-English string into a pinyin string */
String str_to_pin (string const & input)
{
String result;
Unsigned short char_zh;
Int inputLength = input. length ();
Unsigned char high, low;
For (int I = 0; I <inputLength; ++ I)
{
High = input [I];
If (high <0x80) result. append (1, high );
Else
{
Low = input [++ I];
Char_zh = (high <8) + low;
Result. append (get_pin (char_zh ));
}
}
Return result;
}
/* Convert a Chinese-English string into a pinyin string, followed by a space */
String str_to_pin_space (string const & input)
{
String result;
Unsigned short char_zh;
Int inputLength = input. length ();
Unsigned char high, low;
For (int I = 0; I <inputLength; ++ I)
{
High = input [I];
If (high <0x80)
{
If (I> 0 & input [I-1] <0) result. append (1 ,'');
Result. append (1, high );
}
Else
{
If (I> 0) result. append (1 ,'');
Low = input [++ I];
Char_zh = (high <8) + low;
Result. append (get_pin (char_zh ));
}
}
Return result;
}
/*
Converts a Chinese-English string and a digital string into a pinyin string and places a space between each consecutive part.
The input must only meet the preceding three characters. The correctness is guaranteed by the caller.
*/
String str_to_pin_space _ (string const & input)
{
String result (1 ,'');
Unsigned short char_zh;
Int inputLength = input. length ();
Unsigned char high, low;
For (int I = 0; I <inputLength; ++ I)
{
High = input [I];
If (high <58)
{
If (I> 0 & (input [I-1] <0 | input [I-1]> 57 ))
{
Result. append (1 ,'');
}
Result. append (1, high );
}
Else if (high> 64 & high <123)
{
If (I> 0 & (input [I-1] <0 | input [I-1] <58 ))
{
Result. append (1 ,'');
}
Result. append (1, high );
}
Else
{
If (I> 0) result. append (1 ,'');
Low = input [++ I];
Char_zh = (high <8) + low;
Result. append (get_pin (char_zh ));
}
}
Return result;
}
/* Test code */
Int main ()
{
Cout <str_to_pin_space _ ("zhong hua people's dfd Ah a152 left 35gdaf Republic") <endl;
Return 0;
}
From endless blogs