`
jiaoxujin
  • 浏览: 63916 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

中文与Unicode编码互转

阅读更多
<html>
<head><title>convert</title>
<script language="JavaScript">
function A2U(str) { //ASCII -> Unicode

    if (parseFloat(ScriptEngineMajorVersion() + '.' + ScriptEngineMinorVersion()) < 5.5) {
        alert('Your sciprt engin version is too old, pls update it to over 5.5');
        return;
    }

    var result = '';

    for (var i=0; i < str.length; i++)

    result  += '&#' + str.charCodeAt(i) + ';';
   
    document.getElementById("unicode").value = result;

}


function U2A(str) { //Unicode -> ASCII

    var code = str.match(/&#(\d+);/g);

    if (code == null) {
        return '';
    }

    var result = '';

    for (var i=0; i<code.length; i++)

        result += String.fromCharCode(code[i].replace(/[&#;]/g, ''));

    document.getElementById("chinese").value = result;

}
</script>
</head>
<body>
<p />
<p />
<center>
<input type="box" id="chinese" />
<input type="button" value="chineseToUnicode" onclick="A2U(document.getElementById('chinese').value)">
<br />
<input type="box" id="unicode" />
<input type="button" value="unicodeToChinese" onclick="U2A(document.getElementById('unicode').value)">
</center>
</body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics