日期:2014-05-16  浏览次数:20414 次

JS编码与解码(一)
<html>
<body>
<script type = "text/javascript">
function encodeAll(str) {//转化为十六进制
    var HEX = "0123456789ABCDEF",
    radix = 16,
    len = str.length,
    encodeStr = "";
    for (var i = 0; i < len; i++) {
        var num = parseInt(str.charCodeAt(i), 10);
        encodeStr += "%" + Math.floor(num / radix) + HEX.charAt(num % radix);
    }
    return encodeStr;
}
function decodeAll(encodeStr) {//把十六进制还原为字符串
    var arr = encodeStr.split("%"),
    str = "";
    for (var i = 1; arr[i]; i++) {
        str += String.fromCharCode(parseInt(arr[i], 16));
    }
    return str;
}

//测试代码
var s = "*!+abcdefgikzf!~@#$%^&*()_+";
s = encodeAll(s);
document.write(s + "<br/> " + decodeAll(s));
</script>
</body>
</html>

//以后会完善其可配置。


2楼txr1883昨天 11:03
[https://github.com/zensh/anyBaseConverter](https://github.com/zensh/anyBaseConverter)
1楼txr1883昨天 11:03
可以看看这个,任意进制、任意字符的编码解码