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.