<html>
<body>
<meta http-equiv="Content-Type"content="text/html;charset=utf-8">
<form>
输入16进制:<br>
<textarea type="text" id="fname" onchange="myFunction()"></textarea>
</form>
转换为字符串:<br>
<p id="file"></p>
<script type="text/javascript">
function hexToStr(hex,encoding)
{
var trimedStr = hex.trim();
var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
var len = rawStr.length;
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;
}
function myFunction(){
var x=document.getElementById("fname");
var file=document.getElementById("file");
file.innerText=hexToStr(x.value,"utf-8");
}
</script>
</body>
</html>