zoukankan      html  css  js  c++  java
  • C#中汉字的繁体和简体的相互转换的两个方法!

    实际上在C#中实现繁体和简体的相互转换还是比较容易的,一般有三种方法来实现,我这里总结一下:

    1,可以利用VB.NET中的StrConv方法来实现,由于C#中没有提供这样的方法,只好借用VB.NET里的方法了:

         需要先添加对Microsoft Visual Basic.net runtime.dll程序集的引用

         如果这样来实现转换:

      //繁体转简体   

      MessageBox.Show(Microsoft.VisualBasic.Strings.StrConv("張三",Microsoft.VisualBasic.VbStrConv.SimplifiedChinese,0));  

      //简体转繁体   

      MessageBox.Show(Microsoft.VisualBasic.Strings.StrConv("张三",Microsoft.VisualBasic.VbStrConv.TraditionalChinese,0));



    2,单纯的使用C#也可以实现,方法如下:



       System.Text.Encoding   gb2312=System.Text.Encoding.GetEncoding("gb2312");  

      System.Text.Encoding   big5=System.Text.Encoding.GetEncoding("big5");  

      string   s="这是一个汉字转换测试";  

      byte[]   bGb2312=gb2312.GetBytes(s);  

      byte[]   bBig5=System.Text.Encoding.Convert(gb2312,big5,bGb2312);  

    string result=big5.GetString(bBig5);



    3,也可以利用windows API来实现,定义一个用于转换的静态类ChineseStringConverter,通过LCMapString API来实现转换


    public static class ChineseStringConverter

    {

    internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;

    internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;

    internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;



    [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]

    internal static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);



    public static string TraditionalToSimplified(string source)

    {

    String target = new String(' ', source.Length);

    int ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, source, source.Length, target, source.Length);

    return target;

    }



    public static string SimplifiedToTraditional(string source)

    {

    String target = new String(' ', source.Length);

    int ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, source, source.Length, target, source.Length);

    return target;

    }

    }



    我想到了这几种方法,如果还有其他方法,请大家多多指教啊!


     

  • 相关阅读:
    要发布游戏啦,平台要 3000个6位不重复的验证码 看我怎么做!!!
    数字化婚姻配对尝试
    java 和 .NET 的 类继承方面 的不同
    Asp.Net Core 实现谷歌翻译ApI 免费版
    Asp.Net Core 全局模型验证
    Asp.Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty
    Windows 10 远程连接出现函数错误 【这可能由于CredSSP加密Oracle修正】
    矫正系统时间
    linux 安装WildFly 及可能遇到的问题
    ubuntu 安装 jdk-7
  • 原文地址:https://www.cnblogs.com/xuefeng1982/p/1456121.html
Copyright © 2011-2022 走看看