asp.net converts UTF-8 encoding to GB2312 encoding

Source: Internet
Author: User
Tags modify net
Asp.net| Code | conversion

In a recent system, a problem has been encountered with the UTF-8 encoding of the trading system, while some support systems use GB2312 encoding.

Different coding of the pages, the script to each other, will produce garbled problem, the solution is to unify into a code.
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 is only to modify the overall default encoding, if only a certain page encoding needs to be modified, asp.net can simply use the following code:


Note: Add to the Page_Load () event below
Encoding gb2312 = encoding.getencoding ("gb2312");
response.contentencoding = gb2312;
In a asp.net application, perhaps the data you read is UTF-8 encoding, but you want to convert to GB2312 encoding, you can refer to the following code:

String utfinfo = "document.write (\" alert) (' How are you?? ');\");";
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 illustrate
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 the various other encodings, similar to the code above, is not 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.