Implementation of gb2312 encoding and decoding in js _ javascript skills

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

The Code is as follows:


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 Var c = str. charAt (I );
// + Is a space
If (c = '+ '){
StrOut + = '';
}
// A, B, c, 1, 2, etc., directly return itself if it is not starting with %
Else if (c! = '% '){
StrOut + = c;
}
// Start with %
Else {
I ++;
Var nextC = str. charAt (I );
// Number, not Chinese Characters
If (! IsNaN (parseInt (nextC ))){
I ++;
StrOut + = decodeURIComponent (c + nextC + str. charAt (I ));
}
Else {
Var x = new String ();
Try
{
Var code = str. substr (I, 2) + str. substr (I + 3, 2 );
I = I + 4;
Var index =-1;
While (index = z. indexOf (code, index + 1 ))! =-1 ){
If (index % 4 = 0 ){
StrOut + = String. fromCharCode (index/4 + 19968 );
Break;
}
}
} Catch (e ){}
}
}
}
Return strOut;
}
Var z = '{0 }';


(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

The Code is as follows:


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? 1.1.20 ", str, Encoding. ASCII );

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.