C#繁简转换

//1.
using System.Runtime.InteropServices;

 //2.
import kernel32.dll

[DllImport("kernel32.dll",EntryPoint = "LCMapStringA")] 
      public static extern int LCMapString(int Locale,int dwMapFlags,byte[] lpSrcStr,int cchSrc,byte[] lpDestStr,int cchDest); 
      const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000; 
      const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;
 //3.转简体

private void btnCHS_Click(object sender, EventArgs e) 
        { 
            string src = txtSrcText.Text.Trim(); 
            byte[] srcByte = Encoding.Default.GetBytes(src); 
            byte[] desByte = new byte[srcByte.Length]; 
            LCMapString(2052, LCMAP_SIMPLIFIED_CHINESE, srcByte, -1, desByte, srcByte.Length); 
            string des = Encoding.Default.GetString(desByte); 
            txtDesText.Text = des; 
        }
 //4.转繁体

private void btnCHT_Click(object sender, EventArgs e) 
        { 
            string src = txtSrcText.Text.Trim(); 
            byte[] srcByte = Encoding.Default.GetBytes(src); 
            byte[] desByte = new byte[srcByte.Length]; 
             
            LCMapString(2052, LCMAP_TRADITIONAL_CHINESE, srcByte, -1, desByte, srcByte.Length); 
            string des = Encoding.Default.GetString(desByte); 
            txtDesText.Text = des; 
        }
===============第二种=====================
//1. 参考Microsoft.VisualBasic.dll

//2. 转简体代码

private void btnCHS_Click(object sender, EventArgs e) 
        { 
            string src = txtSrcText.Text.Trim(); 
            string des = Microsoft.VisualBasic.Strings.StrConv(src, Microsoft.VisualBasic.VbStrConv.SimplifiedChinese, 0); 
            txtDesText.Text = des; 
        }
//3. 转繁体代码

private void btnCHT_Click(object sender, EventArgs e) 
        { 
            string src = txtSrcText.Text.Trim(); 
            string des = Microsoft.VisualBasic.Strings.StrConv(src, Microsoft.VisualBasic.VbStrConv.TraditionalChinese, 0); 
            txtDesText.Text = des; 
        }

posted @ 2012-02-03 10:54  @张凯@  阅读(355)  评论(0编辑  收藏  举报