汉字拼音声母计算类

 1 using System;
2 using System.Text;
3
4 ///<summary>
5 ///汉字拼音声母计算类
6 ///</summary>
7 public class ChineseConvertor
8 {
9 private ChineseConvertor() { }
10
11 ///<summary>
12 ///获取一串汉字的拼音声母
13 ///</summary>
14 ///<param name="chinese">Unicode格式的汉字字符串</param>
15 ///<returns>拼音声母字符串</returns>
16 ///<example>
17 ///“获取一串汉字的拼音声母”转换为“hqychzdpyszm”
18 ///</example>
19 public static String Convert(String chinese)
20 {
21 chinese = chinese.Replace("", "");
22 char[] buffer = new char[chinese.Length];
23 for (int i = 0; i < chinese.Length; i++)
24 {
25 buffer[i] = Convert(chinese[i]);
26 }
27 return new String(buffer);
28 }
29
30 ///<summary>
31 ///获取一个汉字的拼音声母
32 ///</summary>
33 ///<param name="chinese">Unicode格式的一个汉字</param>
34 ///<returns>汉字的声母</returns>
35 public static char Convert(Char chinese)
36 {
37 if (chinese == char.MinValue)
38 {
39 return '\0';
40 }
41 Encoding gb2312 = Encoding.GetEncoding("GB2312");
42 Encoding unicode = Encoding.Unicode;
43
44 // Convert the string into a byte[].
45 byte[] unicodeBytes = unicode.GetBytes(new Char[] { chinese });
46 // Perform the conversion from one encoding to the other.
47 byte[] asciiBytes = Encoding.Convert(unicode, gb2312, unicodeBytes);
48
49 // 计算该汉字的GB-2312编码
50 int n = (int)asciiBytes[0] << 8;
51 if (asciiBytes.Length > 1)
52 {
53 n += (int)asciiBytes[1];
54 }
55 else
56 {
57 return '\0';
58 }
59 // 根据汉字区域码获取拼音声母
60 if (In(0xB0A1, 0xB0C4, n)) return 'a';
61 if (In(0XB0C5, 0XB2C0, n)) return 'b';
62 if (In(0xB2C1, 0xB4ED, n)) return 'c';
63 if (In(0xB4EE, 0xB6E9, n)) return 'd';
64 if (In(0xB6EA, 0xB7A1, n)) return 'e';
65 if (In(0xB7A2, 0xB8c0, n)) return 'f';
66 if (In(0xB8C1, 0xB9FD, n)) return 'g';
67 if (In(0xB9FE, 0xBBF6, n)) return 'h';
68 if (In(0xBBF7, 0xBFA5, n)) return 'j';
69 if (In(0xBFA6, 0xC0AB, n)) return 'k';
70 if (In(0xC0AC, 0xC2E7, n)) return 'l';
71 if (In(0xC2E8, 0xC4C2, n)) return 'm';
72 if (In(0xC4C3, 0xC5B5, n)) return 'n';
73 if (In(0xC5B6, 0xC5BD, n)) return 'o';
74 if (In(0xC5BE, 0xC6D9, n)) return 'p';
75 if (In(0xC6DA, 0xC8BA, n)) return 'q';
76 if (In(0xC8BB, 0xC8F5, n)) return 'r';
77 if (In(0xC8F6, 0xCBF0, n)) return 's';
78 if (In(0xCBFA, 0xCDD9, n)) return 't';
79 if (In(0xCDDA, 0xCEF3, n)) return 'w';
80 if (In(0xCEF4, 0xD188, n)) return 'x';
81 if (In(0xD1B9, 0xD4D0, n)) return 'y';
82 if (In(0xD4D1, 0xD7F9, n)) return 'z';
83 return '\0';
84 }
85
86 private static bool In(int Lp, int Hp, int Value)
87 {
88 return ((Value <= Hp) && (Value >= Lp));
89 }
90 }
posted @ 2011-11-16 09:40  Relict  阅读(321)  评论(0编辑  收藏  举报