In C #, the traditional and simplified Chinese characters are converted to each other!

Source: Internet
Author: User

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!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.