C # convert the encoding methods of gb2312 and utf8!

Source: Internet
Author: User

C # implement gb2312 and utf8 character encoding

Public String utf8togb2312 (string Str)
{
Try
{
Encoding utf8 = encoding. getencoding (65001 );
Encoding gb2312 = encoding. getencoding ("gb2312"); // encoding. Default, 936
Byte [] temp = utf8.getbytes (STR );
Byte [] temp1 = encoding. Convert (utf8, gb2312, temp );
String result = gb2312.getstring (temp1 );
Return result;
}
Catch (exception ex) // (unsupportedencodingexception ex)
{
MessageBox. Show (ex. tostring ());
Return NULL;
}
}
Public String gb2312toutf8 (string Str)
{
Try
{
Encoding uft8 = encoding. getencoding (65001 );
Encoding gb2312 = encoding. getencoding ("gb2312 ");
Byte [] temp = gb2312.getbytes (STR );
MessageBox. Show ("number of bytes encoded by gb2312:" + temp. Length );
For (INT I = 0; I <temp. length; I ++)
{
MessageBox. Show (convert. touint16 (temp [I]). tostring ());
}
Byte [] temp1 = encoding. Convert (gb2312, uft8, temp );
MessageBox. Show ("Number of uft8 encoded Bytes:" + temp1.length );
For (INT I = 0; I <temp1.length; I ++)
{
MessageBox. Show (convert. touint16 (temp1 [I]). tostring ());
}
String result = uft8.getstring (temp1 );
Return result;
}
Catch (exception ex) // (unsupportedencodingexception ex)
{
MessageBox. Show (ex. tostring ());
Return NULL;
}
}

CodeNote:

Encoding utf8 = encoding. getencoding (65001); // use code page

Encoding gb2312 = encoding. getencoding ("gb2312"); // use bodyname

Get character encoding byte sequence: byte [] temp = utf8.getbytes ("http://www.my400800.cn
400 telephone reception
");

Encoding method conversion: byte [] temp1 = encoding. Convert (utf8, gb2312, temp );

Obtain the encoded string: String str1 = gb2312.getstring (temp1 );

This completes character encoding conversion.

Encoding. Default is generally in gb2312 format in simplified Chinese OS.

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.