ascaii和string互转
2012-05-22 15:37:58 来源:WEB开发网核心提示:function StringBuffer(){this.arr=[];}StringBuffer.prototype.append=function(str){this.arr.push(str);return this;};StringBuffer.prototype.toString=function(str){
function StringBuffer(){ this.arr=[]; } StringBuffer.prototype.append=function(str){ this.arr.push(str); return this; }; StringBuffer.prototype.toString=function(str){ return this.arr.join(''); }; //字符转ascaii function string2ascaii(str){ var sb=new StringBuffer(); for(var i=0;i<str.length;i++){ var ch=str.charAt(i); if(/[\x00-\xff]/i.test(ch)){ sb.append("\\").append(parseInt(ch.charCodeAt(0)).toString(8)); }else{ sb.append("\\u").append(parseInt(ch.charCodeAt(0)).toString(16)); } } return sb.toString(); } //ascaii 转字符 function ascaii2string(str){ if(str==null){ return str; } str=str.replace(/\\([1-7][0-7]?[0-7]?)/ig,function($0,$1){ return String.fromCharCode(parseInt($1,8)); }).replace(/\\u([1-9a-f][0-9a-f][0-9a-f][0-9a-f]?)/ig,function($0,$1){ return String.fromCharCode(parseInt($1,16)); }); return str; }
更多精彩
赞助商链接