谁能帮我解释下这段JS代码?谢谢 ( 积分: 200 )

  • 主题发起人 主题发起人 hyj_it
  • 开始时间 开始时间
H

hyj_it

Unregistered / Unconfirmed
GUEST, unregistred user!
function utf8(wide) {
var c, s;
var enc = "";
var i = 0;
while(i<wide.length) {
c= wide.charCodeAt(i++);
//返回第I个字符的UNICODE编码
// handle UTF-16 surrogates
if (c>=0xDC00 &amp;&amp;
c<0xE000) continue;
if (c>=0xD800 &amp;&amp;
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&amp;0x3F));
else
if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&amp;0x3F),0x80+(c&amp;0x3F));
else
enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&amp;0x3F),0x80+(c>>6&amp;0x3F),0x80+(c&amp;0x3F));
}
return enc;
}
var hexchars = &quot;0123456789ABCDEF&quot;;

function toHex(n) {
return hexchars.charAt(n>>4)+hexchars.charAt(n &amp;
0xF);
>> 右移
}
var okURIchars = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-&quot;;

function encodeURIComponentNew(s) {
var s = utf8(s);
var c;
var enc = &quot;&quot;;
for (var i= 0;
i<s.length;
i++) {
if (okURIchars.indexOf(s.charAt(i))==-1)
enc += &quot;%&quot;+toHex(s.charCodeAt(i));
else

enc += s.charAt(i);
}
return enc;
}
 

Similar threads

后退
顶部