解码和编码

来源:互联网 发布:adsafe净网大师mac版 编辑:程序博客网 时间:2024/06/07 23:44
<span style="font-size:18px;">//获取字符,转为unicodevar msg="今晚308,w84u";function encode(msg){ //i从0开始,到<msg的length结束,同时声明空字符串codesfor(var i=0,codes="";i<msg.length;i++){//调用msg的charCodeAt方法,将i位置的字符转为unicode,并拼接到codes中var code=msg.charCodeAt(i);//code+100000+"",截取从1开始到结尾的剩余字符,再拼接到codes中codes+=(code+100000+"").slice(1);}//(遍历结束)return codes;}function decode(codes){//i从0开始,到<codes的length结束,i每次增5,同时声明空字符串msgfor(var i=0,msg="";i<codes.length;i+=5){//截取codes中i位置到i+5位置的子字符串,保存在变量code中var code=codes.slice(i,i+5);//用fromCharCode将code转为字符,并拼接到msg中msg+=String.fromCharCode(code);}//(遍历结束)return msg;}//测试: msg=encode(msg);console.log(msg);//unicodemsg=decode(msg);console.log(msg);//原文</span>

0 0