C#. NET Frequently asked Questions (FAQ)-How to change string encoding

Source: Internet
Author: User

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 encoded bytes of 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 encoded bytes of UFT8:" + 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;
??????????? }
??????? }

Code Description:

Encoding UTF8 = encoding.getencoding (65001);//Use code page

Encoding gb2312 = encoding.getencoding ("gb2312");//Through Bodyname

Gets the character encoding byte sequence: byte[] Temp=utf8. GetBytes (str);

Encoding mode conversion: byte[] Temp1=encoding.convert (UTF8, gb2312, temp);

Gets the encoded string: string str1=gb2312. GetString (TEMP1);

This completes the conversion of the character encoding.

Encoding.default is generally the gb2312 format in the Simplified Chinese OS.

?

?

For more instructional videos and downloads, please pay attention to the following information:

My Youku Space:

Http://i.youku.com/acetaohai123

?

My online forum:

http://csrobot.gz01.bdysite.com/

?

Problem Exchange:

qq:910358960

Email:[email protected]

?

?

C#. NET Frequently asked Questions (FAQ)-How to change string encoding

Related Article

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.