首页 > 编程知识 正文

汉字拼音编码,中文字符的拼音码

时间:2023-05-04 15:53:01 阅读:211257 作者:2586

方法一:

 

public static string GetCodstring(string UnName) { int i = 0; ushort key = 0; string strResult = string.Empty; //创建两个不同的encoding对象 Encoding unicode = Encoding.Unicode; //创建GBK码对象 Encoding gbk = Encoding.GetEncoding(936); //将unicode字符串转换为字节 byte[] unicodeBytes = unicode.GetBytes(UnName); //再转化为GBK码 byte[] gbkBytes = Encoding.Convert(unicode, gbk, unicodeBytes); while (i < gbkBytes.Length) { //如果为数字/字母/其他ASCII符号 if (gbkBytes[i] <= 127) { strResult = strResult + (char)gbkBytes[i]; i++; } #region 否则生成汉字拼音简码,取拼音首字母 else { key = (ushort)(gbkBytes[i] * 256 + gbkBytes[i + 1]); if (key >= '/uB0A1' && key <= '/uB0C4') { strResult = strResult + "A"; } else if (key >= '/uB0C5' && key <= '/uB2C0') { strResult = strResult + "B"; } else if (key >= '/uB2C1' && key <= '/uB4ED') { strResult = strResult + "C"; } else if (key >= '/uB4EE' && key <= '/uB6E9') { strResult = strResult + "D"; } else if (key >= '/uB6EA' && key <= '/uB7A1') { strResult = strResult + "E"; } else if (key >= '/uB7A2' && key <= '/uB8C0') { strResult = strResult + "F"; } else if (key >= '/uB8C1' && key <= '/uB9FD') { strResult = strResult + "G"; } else if (key >= '/uB9FE' && key <= '/uBBF6') { strResult = strResult + "H"; } else if (key >= '/uBBF7' && key <= '/uBFA5') { strResult = strResult + "J"; } else if (key >= '/uBFA6' && key <= '/uC0AB') { strResult = strResult + "K"; } else if (key >= '/uC0AC' && key <= '/uC2E7') { strResult = strResult + "L"; } else if (key >= '/uC2E8' && key <= '/uC4C2') { strResult = strResult + "M"; } else if (key >= '/uC4C3' && key <= '/uC5B5') { strResult = strResult + "N"; } else if (key >= '/uC5B6' && key <= '/uC5BD') { strResult = strResult + "O"; } else if (key >= '/uC5BE' && key <= '/uC6D9') { strResult = strResult + "P"; } else if (key >= '/uC6DA' && key <= '/uC8BA') { strResult = strResult + "Q"; } else if (key >= '/uC8BB' && key <= '/uC8F5') { strResult = strResult + "R"; } else if (key >= '/uC8F6' && key <= '/uCBF9') { strResult = strResult + "S"; } else if (key >= '/uCBFA' && key <= '/uCDD9') { strResult = strResult + "T"; } else if (key >= '/uCDDA' && key <= '/uCEF3') { strResult = strResult + "W"; } else if (key >= '/uCEF4' && key <= '/uD188') { strResult = strResult + "X"; } else if (key >= '/uD1B9' && key <= '/uD4D0') { strResult = strResult + "Y"; } else if (key >= '/uD4D1' && key <= '/uD7F9') { strResult = strResult + "Z"; } else { strResult = strResult + "?"; } i = i + 2; } #endregion }//end while return strResult; }

方法二:

 

// 国标码和区位码转换常量static final int GB_SP_DIFF = 160;//存放国标一级汉字不同读音的起始区位码static final int[] secPosValueList = {1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787,3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 4086,4390, 4558, 4684, 4925, 5249, 5600};//存放国标一级汉字不同读音的起始区位码对应读音static final char[] firstLetter = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j','k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's','t', 'w', 'x', 'y', 'z'};//获取一个字符串的拼音码public static String getFirstLetter(String oriStr) {String str = oriStr.toLowerCase();StringBuffer buffer = new StringBuffer();char ch;char[] temp;for (int i = 0; i < str.length(); i++) { //依次处理str中每个字符ch = str.charAt(i);temp = new char[] {ch};byte[] uniCode = new String(temp).getBytes();if (uniCode[0] < 128 && uniCode[0] > 0) { // 非汉字buffer.append(temp);} else {if(uniCode[0]==-93)//全角A的处理buffer.append("a");else if(uniCode[0]==-37)//"圳"的处理,非国标码一级汉字buffer.append("z");else if(uniCode[0]==-29)//"泸"的处理buffer.append("l");else if(uniCode[0]==-18)//"钛"的处理buffer.append("t");elsebuffer.append(convert(uniCode));}}return buffer.toString();}/** 获取一个汉字的拼音首字母。* GB码两个字节分别减去160,转换成10进制码组合就可以得到区位码* 例如汉字“你”的GB码是0xC4/0xE3,分别减去0xA0(160)就是0x24/0x43* 0x24转成10进制就是36,0x43是67,那么它的区位码就是3667,在对照表中读音为‘n’*/static char convert(byte[] bytes) {char result = '-';int secPosValue = 0;int i;for (i = 0; i < bytes.length; i++) {bytes[i] -= GB_SP_DIFF;}secPosValue = bytes[0] * 100 + bytes[1];for (i = 0; i < 23; i++) {if (secPosValue >= secPosValueList[i] && secPosValue < secPosValueList[i + 1]) {result = firstLetter[i];break;}}return result;} 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。