首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

关于编码的一段程序理解,该怎么处理

2012-03-27 
关于编码的一段程序理解function myurlencode(str){len str.lengthres new String()charOrd new N

关于编码的一段程序理解
function myurlencode(str){
  len = str.length;
  res = new String();
  charOrd = new Number();
   
  for (i = 0; i < len; i++) {
  charOrd = str.charCodeAt(i);
  if ((charOrd >= 65 && charOrd <= 90) || (charOrd >= 97 && charOrd <= 122) || (charOrd >= 48 && charOrd <= 57) || (charOrd == 33) || (charOrd == 36) || (charOrd == 95)) {
  // this is alphanumeric or $-_.+!*'(), which according to RFC1738 we don't escape
  res += str.charAt(i);
   
  }
  else {
  res += '%';
  if (charOrd > 255) 
  res += 'u';
  hexValStr = charOrd.toString(16);
  if ((hexValStr.length) % 2 == 1) 
  hexValStr = '0' + hexValStr;
  res += hexValStr;
  }
  }
   
  return res;
}

请问这段程序是要完成一个什么事的,再帮我稍微解答一下ELSE后面的一些语句。

[解决办法]
我想是自定义的类似encodeURI 和encodeURIComponent 的程序

charOrd是i位置的Unicode 编码

res += '%'; 
这个懂吧
if (charOrd > 255)
res += 'u'; 
根据规则 字符值大于 255 的以 %uxxxx 格式存储 
hexValStr = charOrd.toString(16); 
返回charOrd的16进制形式
if ((hexValStr.length) % 2 == 1)
hexValStr = '0' + hexValStr; 
如果长度是单数就在前面加0
res += hexValStr; 
这个懂吧
[解决办法]
(charOrd > = 65 && charOrd <= 90) ¦ ¦ (charOrd > = 97 && charOrd <= 122) 表示charOrd是大写或小写字母。

热点排行
Bad Request.