编码转换源码
html
<html>
<head>
<meta charset="utf-8"/>
<title>转换工具</title>
</head>
<body>
<div>
<h3>Unicode编码转换</h3><br/>
编码前:<textarea id="unicode_convert_plaintext"></textarea>
编码后:<textarea id="unicode_convert_encoded"></textarea>
<select id="slc_unicode_prefix">
<option value="%">%</option>
<option value="\">\</option>
</select>
<input type="button" value="编码" id="btn_unicode_encode" />
<input type="button" value="解码" id="btn_unicode_decode" />
</div>
</body>
<script>
window.addEventListener("click",function(e){
switch(e.target.id){
case "btn_unicode_encode":
var inputStr = document.getElementById("unicode_convert_plaintext").value;
var encodePrefix = document.getElementById("slc_unicode_prefix").value;
var encodedStr = unicodeEncode(inputStr, encodePrefix);
document.getElementById("unicode_convert_encoded").value=encodedStr;
break;
case id="btn_unicode_decode" :
var inputStr = document.getElementById("unicode_convert_encoded").value;
var decodedStr = unicodeDecode(inputStr);
document.getElementById("unicode_convert_plaintext").value=decodedStr;
break;
}
});
function unicodeEncode(str, prefix){
var outstr="";
for(var i =0;i<str.length;i++) {
var thisChar=str.charCodeAt(i).toString(16);
var thisCharLength = thisChar.length;
for(var padp=0;padp<4-thisCharLength;padp++) {
thisChar = "0"+thisChar;
}
outstr = outstr + prefix+"u"+thisChar;
}
return outstr;
}
function unicodeDecode(str){
return unescape(str.replace(/\\u/g,'%u'));
}
</script>
</html>