Asp.net converts the UTF-8 encoding to gb2312 Encoding

Source: Internet
Author: User

Recently in the system, encountered a problem, the transaction system using UTF-8 encoding, and some support systems are using gb2312 encoding.

If pages and scripts of different codes are referenced from each other, garbled characters are generated.
In Asp.net, if you want to modify the encoding of the output page, you can modify the following configuration information in Web. config:

<Globalization requestencoding = "UTF-8" responseencoding = "UTF-8"/>
The above only modifies the overall default encoding. If only the encoding of a page needs to be modified, Asp.net can simply use the followingCode:

Note: add it to the page_load () event.
Encoding gb2312 = encoding. getencoding ("gb2312 ");
Response. contentencoding = gb2312;
In non-Asp.net applications, the data you may read is UTF-8 encoding, but to convert to gb2312 encoding, you can refer to the following code:

 

String utfinfo = "document. Write (\" alert ('Are you okay ?? ');\");";
String gb2312info = string. empty;

Encoding utf8 = encoding. utf8;
Encoding gb2312 = encoding. getencoding ("gb2312 ");

// Convert the string into a byte [].
Byte [] unicodebytes = utf8.getbytes (utfinfo );
// Perform the conversion from one encoding to the other.
Byte [] asciibytes = encoding. Convert (utf8, gb2312, unicodebytes );

// Convert the new byte [] into a char [] and then into a string.
// This is a slightly different approach to converting to pair strate
// The use of getcharcount/getchars.
Char [] asciichars = new char [gb2312.getcharcount (asciibytes, 0, asciibytes. Length)];
Gb2312.getchars (asciibytes, 0, asciibytes. length, asciichars, 0 );
Gb2312info = new string (asciichars );

Of course, the conversion between other types of codes is similar to the above Code and will not be described.

 

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.