Convert UTF-8 encoding to gb2312 Encoding

Source: Internet
Author: User
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:
 
  <GlobalizationRequestencoding= "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:

  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 ('aa, 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 revoke 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 ); 

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.