js中漢字utf8編碼互相轉換


//UTF字符轉換
var UTFTranslate = {
Change:function(pValue){
return pValue.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"&#x$2;")});
},
ReChange:function(pValue){
return unescape(pValue.replace(/&#x/g,'%u').replace(/\\u/g,'%u').replace(/;/g,''));
}
};
例如:
UTFTranslate.ReChange('中文sdfdf'); //"中文sdfdf"
UTFTranslate.Change('中文sdfsf'); // "中文sdfsf"
//Asc轉換
var AscTranslate = {
Change: function(pAscString, pBoxId){
/* 轉ASCII */
box = document.getElementById(pBoxId);
box.innerHTML = '';
for(i = 0; i < pAscString.length; i++)
{
var asc = pAscString.charCodeAt(i);
var str = String.fromCharCode(asc);
box.innerHTML += str + ':' + asc + '<br/>';
}
}
}

 轉自:http://www.cnblogs.com/if404/archive/2012/05/13/2498111.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM