本站首页    管理页面    写新日志    退出

The Neurotic Fishbowl

[.NET-Winform]C#中实现GBK码与Unicode码的转换及拼音简码的生成
麦林 发表于 2008/9/2 17:40:21

        /// <summary>        /// 生成拼音简码        /// </summary>        /// <param name="unicodeString">Unicode编码字符串</param>        /// <returns>拼音简码:string</returns>        public static string GetPinyinCode(string unicodeString)        {            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(unicodeString);            //再转化为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;        }完全看不懂if里面的那些字符是什么东东,只好收藏啦~

阅读全文(5931) | 回复(1) | 编辑 | 精华

 


回复:C#中实现GBK码与Unicode码的转换及拼音简码的生成
徐涵(Han Xu)发表评论于2008/9/2 23:08:30

好久不见麦林! 以下为blog主人的回复:是哦,貌似一个多月了,都忘了的。

个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除

 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
 

The Neurotic Fishbowl

.: 公告

fighting for the work!


Bloginess

«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031

.: 我的分类(专题)

首页(449)
文字感悟(103)
学习公共库(61)
生活体会(100)
资源收集(14)
.NET-Winform(27)
音乐文字(15)
心情小记(119)
SQL问题解决(10)


In the Bowl

.: 最新日志

C#打印代码
你看到的我是蓝色的
即将逝去的3月
这个假期
两个月过去
表名存在其他表时获取数据
条码打印
Remoting的用法
INI文件用法


.: 最新回复

回复:条码打印
回复:条码打印
回复:即将逝去的3月
回复:cookie对象使用
回复:条码打印
回复:JSP学习(字符串比较)
回复:应收应付核销规则及常见问题(续)
回复:两个月过去
回复:两个月过去


The Fishkeeper
blog名称:栗色?蓝色?
日志总数:449
评论数量:201
留言数量:37
访问次数:2265174
建立时间:2006年5月16日



Text Me

.: 留言板

签写新留言

链接完成
回:loseVC
来喽~
转转......
新年啦
对自己说
祝福
回:佑手
WO


Other Fish in the Sea

.: 链接


loseVC's BLOG
Collapsar_feel
布衣加针
C#and.Net
Kinogam Web




站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.063 second(s), page refreshed 144801119 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号