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.