Implementation of gb2312 encoding and decoding in JS

Source: Internet
Author: User

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.
Therefore, JSCodeAs follows:

Code Copy 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 <Str. length; 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 copy 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 {< br> 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 ();
}< br> string STR = strformat + sb. tostring (0, sb. length-1) + "';";
system. io. file. writealltext (@ "F: \ encodegb2312.js", STR, encoding. ASCII);

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.