1. Write Chinese Characters in Asp.net's httpcookie. garbled characters are read In fact, this is caused by text encoding. Chinese characters are encoded in two ways, so such a garbled code is generated! In fact, the solution is very simple: as long as the cookie is written, it will first use URL encoding and then write, and then it will be OK when we read it.Example: Cookie writing: Httpcookie cookie = new httpcookie ("simple "); Cookie. Values. Add ("simple1", httputility. urlencode ("Chinese character test! ")); Cookie. Values. Add ("simple2", "The English charactor test! "); Response. appendcookie (cookie ); Cookie reading: Httpcookie cookie = request. Cookies ["simple"]; String simple1 = httputility. urldecode (cookie ["simple1"]); String simple2 = cookie ["simple2"]; This way Simple1 = "Chinese character test! "; Simple2 = "The English charactor test! "; 2. Passing parameters with Chinese characters in the address bar When passing a parameter, encode it with urlencode and then assign it to the URL Connection to avoid garbled characters when obtaining the Chinese parameter; Example: String text = "passing parameters with Chinese characters in the address bar" TEXT = system. Web. httputility. urlencode (text, system. Text. encoding. getencoding ("gb2312 ")); Iii. Encoding Problems During Excel Export ......
4. Convert UTF-8 encoding to gb2312 Encoding Recently in the advertising system, encountered a problem, the advertising system uses the UTF-8 code, and some use this advertising system channel page is gb2312 encoding. Of course there are also channels using UTF-8 encoding using this set of advertising systems. Channel pages call advertisements by embedding code similar to the following. The specific time at which the advertisement is displayed, or the combination of the ADS is handled by the advertising system itself. <SCRIPT type = "text/JavaScript"> <! -- Csdn_ad_position_groupid = "{f05ff3bf-246b-4d71-a101-b5d4ee3f6cd3 }"; Csdn_ad_page_url = Document. location; // --> </SCRIPT> <SCRIPT type = "text/JavaScript" src = http://ads.csdn.net/AD/Show_js_AD.js> </SCRIPT>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 following code:
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 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. V. ASP. NET automatic identification of gb2312 files with UTF-8 Encoding Http://www.webjx.com update date: Question In a simplified Chinese system, we sometimes need to open a plain text file stored on the disk, such as txt, but do not know its storage encoding. What should we do? If the text only needs to be displayed on Windows, it's lucky because both gb2312 and UTF-8-encoded strings are correctly displayed. However, if you need to output data to ASP. NET page is not that simple, because if the Page code for the UTF-8 but the plain text file imported string is gb2312, it will cause garbled, and vice versa. Therefore, we need a method to automatically identify the encoding of plain text files on the disk. Answer Streamreader is actually an automatic encoding detection function, but because it only detects the first three bytes, so only UTF-8, little-Endian Unicode, big-Endian Unicode before making a choice, if none of the above three matches, select the user-provided encoding. Therefore, we can provide gb2312 encoding to streamreader so that it can be automatically selected before gb2312 and UTF-8. Using (streamreader reader = new streamreader (path, encoding. Default) { String line; While (line = reader. Readline ())! = NULL) { Filetextbox. innerhtml + = server. htmlencode (line) + ""; } Fileencodingnamelabel. Text = reader. currentencoding. encodingname; }The above Code uses encoding. Default to initialize streamreader. encoding. Default indicates the system's default ANSI encoding, Which is gb2312 in the simplified Chinese system. This write can make the code to maintain a certain degree of compatibility, for example, in the traditional Chinese system will become automatically in the big5 and UTF-8 between the choice. The subsequent code is to use streamreader to read the file content row by row and put it into filetextbox. Finally, the encoding name is obtained through the currentencoding attribute of streamreader and displayed in fileencodingnamelabel. |