日期:2014-05-16 浏览次数:20414 次
<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> //以后会完善其可配置。