Demand
In JS, Chinese is encoded with gb2312. For example, the "I" Code should be "%ce%d2".
Analysis
As you know, encodeURI and encodeURIComponent will use Utf-8 code, such as "I" Code is "%e6%88%91". According to the experiment, there seems to be no parameter to specify where to encode. Only another way to find him.
There are several solutions to the rough analysis:
1. Create a hidden iframe with JS and specify it as GB2312 encoding, place the text that needs to be converted into an input in the form of the IFRAME, designate the form as Get method and submit it,
Then get its URL and parse it, it should be able to get its gb2312 encoded text.
2. Upload the code to the server with Ajax and send it back.
3. Create a gb2312 code table in JS.
Implement
The first scheme is too frustrating for individuals to test in several different browsers.
The second scenario requires a server collaboration.
The following is the implementation of the third scenario:
Initially intended to use arrays to store encoded tables, and later to reduce the JS file size, instead of using string to store.
So, the JS code is as follows:
Code
Copy Code code 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 and so on, not a% of the beginning, directly return itself
else if (c!= '% ') {
Strout + = C;
}
% start
else{
i++;
var nextc = Str.charat (i);
Numbers, then 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} ';
(Do not consider Chinese punctuation, mainly to see the Chinese punctuation in Unicode and Japanese and Korean punctuation together, distributed in a few places, do not bother to get.) Who can send me a copy, thank you. )
Last use. NET generates the code at Z:
Code
Copy Code code 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", str, ENCODING.ASCII);