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