In fact, it is easier to implement mutual conversion between traditional Chinese and simplified Chinese in C #. There are generally three methods to achieve this. Here I will summarize:
1. You can use the StrConv method in VB. NET to implement it. Since C # does not provide such a method, you have to borrow the method in VB. NET:
You must first Add a reference to the Microsoft Visual Basic.net runtime. dll assembly.
If the conversion is implemented as follows:
// Convert traditional Chinese to simplified Chinese
MessageBox. Show (Microsoft. VisualBasic. Strings. StrConv ("Example 3", Microsoft. VisualBasic. VbStrConv. SimplifiedChinese, 0 ));
// Convert simplified to traditional Chinese
MessageBox. Show (Microsoft. VisualBasic. Strings. StrConv ("Zhang San", Microsoft. VisualBasic. VbStrConv. TraditionalChinese, 0 ));
2. You can simply use C #. The method is as follows:
System. Text. Encoding gb2312 = System. Text. Encoding. GetEncoding ("gb2312 ");
System. Text. Encoding big5 = System. Text. Encoding. GetEncoding ("big5 ");
String s = "this is a Chinese character conversion test ";
Byte [] bGb2312 = gb2312.GetBytes (s );
Byte [] bBig5 = System. Text. Encoding. Convert (gb2312, big5, bGb2312 );
String result = big5.GetString (bBig5 );
3. You can also use the windows API to define a static ChineseStringConverter class for conversion, and use the LCMapString API to implement conversion.
Public static class ChineseStringConverter
{
Internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
Internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
Internal const int lcmap_traditional_chinese.0 x 04000000;
[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;
}
}
I have come up with these methods. If you have other methods, please kindly advise!