<html> <body> <script type="text/javascript"> function myFunction(){ var x=document.getElementById("fname"); var file=document.getElementById("file"); file.innerText=hexToStr(x.value,"utf-8"); } function hexToStr(hex,encoding) { var trimedStr = hex.trim(); var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr; var len = rawStr.length; if (len % 2 !== 0) { //alert("Illegal Format ASCII Code!"); //return ""; } var curCharCode; var resultStr = []; for (var i = 0; i < len; i = i + 2) { curCharCode = parseInt(rawStr.substr(i, 2), 16); resultStr.push(curCharCode); } // encoding为空时默认为utf-8 var bytesView = new Uint8Array(resultStr); var str = new TextDecoder(encoding).decode(bytesView); return str; } </script> <form> 输入16进制:<br> <textarea type="text" id="fname" onchange="myFunction()"></textarea> </form> 转换为字符串:<br> <p id="file"></p> </body> </html>