一:上图,不清楚的看代码注解,很详细了

二:具体代码

窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions; namespace ChineseToABC
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
} private void txt_Chinese_TextChanged(object sender, EventArgs e)
{
txt_PinYIn.Text = //调用拼音类的GetABC方法得到拼音字符串
new PinYin().GetABC(txt_Chinese.Text);
}
}
}
拼音类代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms; public class PinYin
{
//定义拼音区编码数组
private static int[] getValue = new int[]
{
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-, -,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-,
-,-,-,-,-,-,-,-,-,-,-,-
};
//定义拼音数组
private static string[] getStr = new string[]
{
"A","Ai","An","Ang","Ao","Ba","Bai","Ban","Bang","Bao","Bei","Ben",
"Beng","Bi","Bian","Biao","Bie","Bin","Bing","Bo","Bu","Ba","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","Hen","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"
};
/// <summary>
/// 将汉字转换拼音的方法
/// </summary>
/// <param name="str">汉字字符串</param>
/// <returns>拼音字符串</returns>
public string GetABC(string str)
{
Regex reg = new Regex("^[\u4e00-\u9fa5]$");//实力化一个正则表达式,验证输入是否为汉字
byte[] arr = new byte[];//定义字节数组,//每个汉字为2字节
string pystr = "";//定义字符串变量用于返回拼音
char[] mChar = str.ToCharArray();//获取汉字对应的字符数组//将此实例中的字符复制到 Unicode 字符数组。
return GetStr(mChar,pystr,reg,arr);//返回获取到的汉字拼音
}
/// <summary>
/// 拼音字符串
/// </summary>
/// <param name="mChar">汉字对应的字符数组</param>
/// <param name="pystr">用于返回拼音的字符串数组</param>
/// <param name="reg">汉字的正则表达式</param>
/// <param name="arr">定义的字节数组</param>
/// <returns></returns>
private string GetStr(char[] mChar,string pystr,Regex reg,byte[] arr)
{
int asc = , M1 = , M2 = ;
for (int j = ; j < mChar.Length; j++)
{
if (reg.IsMatch(mChar[j].ToString()))//如果输入的是汉字
{
//根据系统默认编码得到字节码
arr = System.Text.Encoding.Default.GetBytes(mChar[j].ToString());
M1 = (short)(arr[]);//取字节1
M2 = (short)(arr[]);//取字节2
//计算汉字的编码,第一个字节移8位后与第二个字节相加得到汉字编码,Unicode固定为双字节编码,共可以表示65536个字符 。
asc = M1 * + M2 - ;
if (asc > && asc < )
{
pystr += mChar[j];
}
else
{
switch (asc)
{
case -:
pystr += "Zhen"; break;
case -:
pystr += "Qian"; break;
case -:
pystr += "Jia"; break;
case -:
pystr += "Ge"; break;
case -:
pystr += "Ga"; break;
case -:
pystr += "La"; break;
case -:
pystr += "Chen"; break;
case -:
pystr += "Mao"; break;
case -:
pystr += "Mao"; break;
case -:
pystr += "Die"; break;
case -:
pystr += "Tian"; break;
default:
for (int i = (getValue.Length - ); i >= ; i--)
{
if (getValue[i] <= asc)//判断汉字的拼音区编码是否在指定范围内
{
pystr += getStr[i];//如果不超出范围则获取对应的拼音
break;
}
}
break;
}
}
}
else//如果不是汉字
{
pystr += mChar[j].ToString();//如果不是汉字则返回
}
}
return pystr;
} }

将汉字转化为拼音,正则表达式和得到汉字的Unicode编码的更多相关文章

  1. C# 将汉字转化成拼音

    本文来自http://www.cnblogs.com/yazdao/archive/2011/06/04/2072488.html 首先下载Visual Studio International Pa ...

  2. c#把汉字转化成全拼音函数(全拼)

    /// <summary>        /// 把汉字转换成拼音(全拼)        /// </summary>        /// <param name=&q ...

  3. HTML5 汉字转化为拼音,带读声,穷举多音字

    1,没别的,像这种没有规则的转化,我们首先需要一个字典文件,字典文件的完整度,决定了转化的成功率与精确度 2,笔者收集了较为完整的字典文件,已上传到博客园,欢迎补充  =>  https://b ...

  4. 利用python将表格中的汉字转化为拼音

    缺少包时用pip install 进行安装,例如: pip install xlsxwriter   完成代码如下: #!/usr/bin/python #-*-coding:utf-8-*- #fr ...

  5. 将汉字转化为拼音的js插件

    /*---------------------------------------------------------------- // 文件名:chinese2pinyin.js // 文件功能描 ...

  6. react下将输入的汉字转化为拼音

    1.首先需要一个简单的拼音和汉字对应的字典文件: /** * 收录常用汉字6763个,不支持声调,支持多音字,并按照汉字使用频率由低到高排序 */ var pinyin_dict_notone = { ...

  7. js如何将汉字转化为拼音

    github地址,上面有封装好的转换工具:https://github.com/sxei/pinyinjs 里面有几个库,根据功能,库的文件大小也不一样,可以根据需求去引入使用. 里面封装好了方法: ...

  8. sql标量值函数,将汉字转化为拼音,无音标

    USE [db_Test]GO SET ANSI_NULLS ONGO SET QUOTED_IDENTIFIER ONGO create function [dbo].[fn_GetPinyin]( ...

  9. 通过类库ChineseChar实现将汉字转化为拼音

    //封装dllusing Microsoft.International.Converters.PinYinConverter;using System.Text;namespace Utils{ p ...

随机推荐

  1. spring xml 空模板-applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  2. sql知识点的积累和使用过的例子

    越来越发现自己的sql方面的知识的欠缺,所以只能放低姿态一点一点的学了 一 游标和charIndex的使用. 游标我一直没用过,以前只是在同事们写的存储过程里见过,但是一直没看明白(可是我就是比较笨吧 ...

  3. Android中dip,dp,sp,pt和px的区别

    dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖像素. ...

  4. Notepad++在编程使用时的小技巧

    http://blog.csdn.net/freewaywalker/article/details/8010790 为了编程时更快捷和适应个人习惯,我们可以对Notepad++进行一系列的设置,这当 ...

  5. Struts2 单个文件上传/多文件上传

    1导入struts2-blank.war所有jar包:\struts-2.3.4\apps\struts2-blank.war 单个文件上传 upload.jsp <s:form action= ...

  6. 关于@property()的那些属性及ARC简介【nonatomic,atomic,assign,retain,strong,weak,copy。】

    @property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...

  7. 第一次用Github desktop(mac)提交代码遇到的问题

    1.新建代码仓库 2.生成密钥 ssh-keygen -C 'your@email.address' -t rsa 3.到根目录下的.ssh文件夹下找到id_rsa.pub文件,将里面的内容复制到下图 ...

  8. $.cookie('name', null) 删除cookie 失效问题

    这几天在做网站的用户登录和退出功能,我使用jquery.cookie.js来操作cookie,但是用$.cookie('name', null) 总是失效, 后来使用$.cookie("na ...

  9. iframe的缺点与优点?

    iframe是一种框架,也是一种很常见的网页嵌入方式. iframe的优点: iframe能够原封不动的把嵌入的网页展现出来. 如果有多个网页引用iframe,那么你只需要修改iframe的内容,就可 ...

  10. 关于JavaScript的类的继承

    其实最一开始学JS的时候就看过继承的实现.当时只是去试着理解从书上看来的代码段而已.今天又重新思考了一下,感觉这是一个思维探索演进的结果. 继承,即复用. 如果抛开继承的固有思想,让b复用a的成员,最 ...