Cyico collects some methods for converting utf8 to gb2312 and implementing urlencode and urldecode in JavaScript.

Source: Internet
Author: User
Tags javascript urlencode

Http://hi.baidu.com/cyico/blog/item/596e6f1694417d4e20a4e99a.html

Keywords: gb2312 conversion UTF-8 UTF-8 conversion gb2312 JavaScript urlencode decoding hex ASC Chr

In my previous articleArticleIt has implemented the issue of decoding urlencode using JavaScript. I hope you can refer to my previous post "using JavaScript on the clientCodeParse the urlencode string ", maybe that is what you want. The posts I collected below can provide some reference help for you in other aspects. The following are their ownSource codeI am not afraid to tamper with the original words, because I didn't write down your website and name at the time, so I didn't stick it up. Thank you for your patience.

[Post 1]

JavaScript Code Conversion
If you are interested in writing or writing, it has little to do with. net.

I 've been bored over the past few days. I saw the software "ADSL password Terminator". Well, I'm not very satisfied with its functions. I want to write one for me.

I am not familiar with network transmission, but I am still familiar with JavaScript + XMLHTTP. I was writing

When the request is sent, all webpages are UTF-8 encoded, and the code is always running well. However, when an address is scanned

An exception occurred. I figured it out that the webpage was originally encoded as gb2312 and had encountered similar problems before. However, at that time

The solution is the same as using ADO. Record. Now, the Browser fails to create this object. You can only find another solution.

And find these functions,

Leadbbs code

// Convert the received gb2312 code into the corresponding text
Function gb2utf8 (data)
{
VaR glbencode = [];
Gb2utf8_data = data;
ExecScript ("gb2utf8_data = midb (gb2utf8_data, 1)", "VBScript ");
VaR T = escape (gb2utf8_data ). replace (/% u/g ,""). replace (/(. {2 })(. {2})/g, "%$ 2% $1 "). replace

(// % ([A-Z].) % (. {2})/g, "@ $1 $2 ");
T = T. Split ("@");
VaR I = 0, j = T. length, K;
While (++ I <j)
{
K = T [I]. substring (0, 4 );
If (! Glbencode [k])
{
Gb2utf8_char = eval ("0x" + k );
ExecScript ("gb2utf8_char = CHR (gb2utf8_char)", "VBScript ");
Glbencode [k] = escape (gb2utf8_char). substring (1, 6 );
}
T [I] = glbencode [k] + T [I]. substring (4 );
}
Gb2utf8_data = gb2utf8_char = NULL;
Return Unescape (T. Join ("% "));
}
// Encode the text with UTF-8
Function utf8 (WIDE)
{
VaR C, S;
VaR ENC = "";
VaR I = 0;
While (I <wide. length)
{
C = wide. charcodeat (I ++ );
// Handle UTF-16 Surrogates
If (C> = 0xdc00 & C <0xe000) continue;
If (C> = 0xd800 & C <0xdc00)
{
If (I> = wide. Length) continue;
S = wide. charcodeat (I ++ );
If (S <0xdc00 | C> = 0xde00) continue;
C = (c-0xD800) <10) + (s-0xDC00) + 0x10000;
}
// Output value
If (C <0x80)
ENC + = string. fromcharcode (C );
Else if (C <0x800)
ENC + = string. fromcharcode (0xc0 + (C> 6), 0x80 + (C & 0x3f ));
Else if (C <0x10000)
ENC ++ = string. fromcharcode (0xe0 + (C> 12), 0x80 + (C> 6 & 0x3f), 0x80 + (C & 0x3f ));
Else
ENC + = string. fromcharcode (0xf0 + (C> 18), 0x80 + (C> 12 & 0x3f), 0x80 + (C> 6 & 0x3f ), 0x80 +

(C & 0x3f ));
}
Return ENC;
}
VaR hexchars = "0123456789 abcdef ";
Function tohex (N)
{
Return hexchars. charat (n> 4) + hexchars. charat (N & 0xf );
}
VaR okurichars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 _-";
Function encodeuricomponentnew (s)
{
VaR S = utf8 (s );
VaR C;
VaR ENC = "";
For (VAR I = 0; I <S. length; I ++)
{
If (okurichars. indexof (S. charat (I) =-1)
ENC + = "%" + tohex (S. charcodeat (I ));
Else
ENC + = S. charat (I );
}
Return ENC;
}

It took me some time to understand these two functions. These two functions are also common on the Internet, but I later considered that if I want to send

What should I do with gb2312 encoding data? So, the crazy search, the hard work, I found two functions, his grandmother is

VBScript version, and the author has no way to write it in VBScript.

The VBScript syntax was changed to Javascript, but in a few places, you have to admit that VBScript is slightly stronger.
Leadbbs code

// Convert to hexadecimal format, and call the hex function of VBScript.
Function hex (N)
{
C = N;
ExecScript ("c = hex (c)", "VBScript ");
Return C;
}
// Ascaii encoding of the returned text, which calls the ASC function of VBScript
Function ASC (s)
{
C = s;
ExecScript ("c = ASC (c)", "VBScript ");
Return C;
}
// Obtain the gb2312 encoding of the text
Function gb2312encode (STR)
{
VaR string = "";
C = s = "";
VaR high = "";
VaR low = "";
For (VAR I = 0; I <Str. length; I ++)
{
C = ASC (Str. charat (I ));
If (math. Abs (c) <0xff)
String + = Str. charat (I );
Else
{
If (C <0) C + = 0x10000;
High = (C & 0xff00)> 8) & 0x00ff;
Low = C & 0xff;
String + = "%" + hex (high) + "%" + hex (low );
}
}
Return string;
}
// Decodes the received gb2312 encoding.
Function gb2312decode (data)
{
String = "";
STR = "";
D = data;
N = "";
C = "";
ExecScript ("L = lenb (d)", "VBScript ");
ExecScript ("d = midb (d, 1)", "VBScript ");
For (I = 1; I <= L; I ++)
{
ExecScript ("c = ASCB (midb (D, I, 1)", "VBScript ");
If (C <0x80)
{
ExecScript ("str = CHR (c)", "VBScript ");
String + = STR;
}
Else
{
ExecScript ("n = ASCB (midb (D, I + 1, 1)", "VBScript ");
ExecScript ("str = CHR (clng (c) * & h100 + CINT (N)", "VBScript ");
String + = STR;
I = I + 1;
}
}
Return string;
}

This is partially original. I believe the Javascript versions of these two functions are not too many. I did not find them. Maybe it's my family.

. Well, if you want to know how these functions work, please learn how gb2312 is encoded and how UTF-8 is used for Unicode characters.

Line encoding.

[Post 2]

(Note: The following code may be found frequently during the search process, but unfortunately, it may not necessarily bring you any substantial progress, here, you can find a method to replace the vbs code and write it into JavaScript, just like using the execScript function in the previous post)

<Script language = "VBScript">
Function str2asc (strstr)
Str2asc = hex (ASC (strstr ))
End Function
Function asc2str (ascasc)
Asc2str = CHR (ascasc)
End Function
</SCRIPT>

<Script language = "JavaScript">
/* Urlencode and urldecode functions at the beginning */
Function urlencode (STR ){
VaR ret = "";
VaR strspecial = "! \ "# $ % & '() * +,/:; <=>? [] ^ '{| }~ % ";
For (VAR I = 0; I <Str. length; I ++ ){
VaR CHR = Str. charat (I );
VaR c = str2asc (CHR );
Tt + = CHR + ":" + C + "N ";
If (parseint ("0x" + C)> 0x7f ){
RET + = "%" + C. Slice (0, 2) + "%" + C. Slice (-2 );
} Else {
If (CHR = "")
RET + = "+ ";
Else if (strspecial. indexof (CHR )! =-1)
RET + = "%" + C. tostring (16 );
Else
RET + = CHR;
}
}
Return ret;
}
Function urldecode (STR ){
VaR ret = "";
For (VAR I = 0; I <Str. length; I ++ ){
VaR CHR = Str. charat (I );
If (CHR = "+ "){
RET + = "";
} Else if (CHR = "% "){
VaR ASC = Str. substring (I + 1, I + 3 );
If (parseint ("0x" + ASC)> 0x7f ){
RET + = asc2str (parseint ("0x" + ASC + Str. substring (I + 4, I + 6 )));
I + = 5;
} Else {
RET + = asc2str (parseint ("0x" + ASC ));
I + = 2;
}
} Else {
RET + = CHR;
}
}
Return ret;
}
Alert (urldecode ("% C2 % D2 % C2 % Eb "));
</SCRIPT>

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.