JavaScript 实现字符与unicode编码的相互转换

这种转换主要用来加密js代码.
这两个函数参考了[脚本之家] (http://www.jb51.net/html/200707/23/10560.htm ) 的代码, 但是他的函数是错的, 我不知道为什么那个作者要那样写, 所以我改写了一下 .

function unicode2Chr(str)
{
 if (str!='')
 {
  var fret, tempStr;
  fret = '';
  for (var i = 1; i <= str.length/4; i ++)
  {
   tempStr = str.slice(4*i-4, 4*i);
   fret = fret.concat('%u').concat(tempStr);
  }
  fret = unescape(fret);
  return(fret);
 }
 else
  return('');
}


function chr2Unicode(str)
{
 if (str!='')
 {
  var fret, tempStr;
  fret = '';
  for (var i = 1; i <= str.length; i ++)
  {
   tempStr = str.charCodeAt(i - 1).toString(16);
   if (tempStr.length < 4)
    while(tempStr.length <4)
         tempStr = '0'.concat(tempStr);
   tempStr = tempStr.slice(0, 4);
   fret = fret.concat(tempStr);
  }
  return(fret.toUpperCase());
 }
 else
  return(''); 
}

原文地址:https://www.cnblogs.com/Moosdau/p/908921.html