<span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 25.200000762939453px;">本人在网上搜索,看到有使用javascript做unicode编码转换的,感觉很好玩,所以拿来使用的。</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 25.200000762939453px;">这个功能有目前测试了两种:</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 25.200000762939453px;">1)unicode互相转换,例如\u4e2d\u56fd转换成字符,支持中文和英文;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 25.200000762939453px;">2)URL编码的解析,例如众所周知的%20是表示空格,还有%24,%33,%44,%62等等;</span><br style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 25.200000762939453px;" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 25.200000762939453px;">我相信,只要你试试上面的字符进行解码,你就会知道他们之间关系,如何表示字符,数字,字母。</span>
<script type="text/javascript">// <![CDATA[
var decToHex = function(str) {
    var res=[];
    for(var i=0;i < str.length;i++)
        res[i]=("00"+str.charCodeAt(i).toString(16)).slice(-4);
    return "\\u"+res.join("\\u");
}
var hexToDec = function(str) {
    str=str.replace(/\\/g,"%");
    return unescape(str);
}
var bianma=function()
{
document.getElementById('code12').value=decToHex(document.getElementById('code11').value);
}
var jiema=function()
{
document.getElementById('code12').value=hexToDec(document.getElementById('code11').value);
}
// ]]></script>

<p><textarea id="code11" style="width: 500px; height: 100px;" rows="5" cols="50" height="100"></textarea> <br />
<input onclick="bianma()" type="button" value="编码" /> 
<input onclick="jiema()" type="button" value="解码" /><br />
<textarea id="code12" style="width: 500px; height: 100px;" rows="5" cols="50"></textarea></p>


本文转载:CSDN博客