Use 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

  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., starting with not %, directly return itself
Else If (C ! = ' % ' ){
Strout + = C;
}
// Starting 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

  Stringbuilder sb  =    New  Stringbuilder ();
String strformat = @ " ... Z =' " ; // Previous 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 );

 

Final JS download: encodetogb2312.zip

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.