Implementation of gb2312 encoding and decoding in js

Source: Internet
Author: User
Use gb2312 to encode Chinese Characters in js. For example, "I" should be encoded with "% CE % D2 ". Requirement
Use gb2312 to encode Chinese Characters in js. For example, "I" should be encoded with "% CE % D2 ".

Analysis
As you know, encodeURI and encodeURIComponent use UTF-8 encoding, for example, "% E6 % 88% 91" after "I" encoding ". According to the experiment, it seems that no parameter specifies the encoding. Only find another method.
There are several solutions as follows:
1. Use js to create a hidden iframe and specify it as gb2312 encoding. Put the text to be converted into an input of the iframe form, specify form as the get method, and submit it,
Then retrieve the url and parse it. You can get the encoded text of gb2312.
2. Upload the code to the server using ajax and then upload it back.
3. Create a gb2312 encoding table in js.

Implementation
The first solution is too difficult for me. I need to test it in multiple different browsers.
The second solution requires the cooperation of servers.
The implementation of the third solution is as follows:
At first, I planned to use arrays to store the encoded table. Later, I used string to store the encoded table to reduce the js file size.
The js Code is as follows:

Code

Function encodeToGb2312 (str) {var strOut = ""; for (var I = 0; I <str. length; I ++) {var c = str. charAt (I); var code = str. charCodeAt (I); if (c = "") strOut + = "+"; else if (code> = 19968 & code <= 40869) {index = code-19968; strOut + = "%" + z. substr (index * 4, 2) + "%" + z. substr (index * 4 + 2, 2);} else {strOut + = "%" + str. charCodeAt (I ). toString (16) ;}} return strOut;} function decodeFromGb2312 (str) {var strOut = ''; for (var I = 0; I
 
  

(Chinese Punctuation is not considered here. It is mainly because the Chinese Punctuation in unicode is mixed with those in Japan and South Korea. If the punctuation is distributed in a few places, you will be too lazy to get it. You can send me a copy if someone else gets it. Thank you .)
Finally, use. NET to generate code at z:

Code

StringBuilder sb = new StringBuilder (); string strFormat = @"... z = '"; // The preceding js Code const int MinHanzi = 19968; const int MaxHanzi = 40869; for (int I = MinHanzi; I <MaxHanzi + 1; I ++) {byte [] bytes = Encoding. getEncoding ("gb2312 "). getBytes (char) I ). toString (); sb. appendFormat ("{0} {1}", Convert. toString (bytes [0], 16 ). toUpper (), Convert. toString (bytes [1], 16 ). toUpper ();} string str = strFormat + sb. toString (0, sb. length-1) + "';"; System. IO. file. writeAllText (@ "F: \ encodeGb2312.js", str, Encoding. ASCII );

For more articles about how to use gb2312 encoding and decoding in js, refer to PHP!

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.