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;

}

}



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


 

转载于:https://www.cnblogs.com/xuefeng1982/archive/2009/05/13/1456121.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值