Convert GB2312 to UTF-8 and utf_8 to GB2312

Source: Internet
Author: User

 

 

/// <Summary>
/// Convert utf_8 to GB2312
/// </Summary>
/// <Param name = "utfInfo"> </param>
/// <Returns> </returns>
Private string ToGB2312 (string utfInfo)
{
String gb2312Info = string. Empty;
Encoding utf8 = Encoding. UTF8;
Encoding gb2312 = Encoding. GetEncoding ("gb2312 ");

Byte [] unicodeBytes = utf8.GetBytes (utfInfo );

Byte [] asciiBytes = Encoding. Convert (utf8, gb2312, unicodeBytes );

Char [] asciiChars = new char [gb2312.GetCharCount (asciiBytes, 0, asciiBytes. Length)];
Gb2312.GetChars (asciiBytes, 0, asciiBytes. Length, asciiChars, 0 );
String gb2312info = new string (asciiChars );
Return gb2312info;
}

/// <Summary>
/// GB2312 to UTF-8
/// </Summary>
/// <Param name = "gbInfo"> </param>
/// <Returns> </returns>
Private string ToUTF8 (string gbInfo)
{
String Info = string. Empty;
Encoding gb2312 = Encoding. GetEncoding ("gb2312 ");
Encoding utf_8 = Encoding. GetEncoding ("UTF-8 ");

Byte [] unicodeBytes = gb2312.GetBytes (gbInfo );

Byte [] asciiBytes = Encoding. Convert (gb2312, utf_8, unicodeBytes );

Char [] asciiChars = new char [utf_8.GetCharCount (asciiBytes, 0, asciiBytes. Length)];
Utf_8.GetChars (asciiBytes, 0, asciiBytes. Length, asciiChars, 0 );
String info = new string (asciiChars );
Return info;

}

 

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.