您的位置:首页 > Web前端 > JavaScript

汉字与Unicode编码相互转换(Js版)

2012-12-07 09:50 543 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>Unicode编码转换工具</title>

<script type="text/javascript">

//ASCII 转换 Unicode

function AsciiToUnicode(){

if (document.getElementById("content").value==""){

alert("文本框中没有代码!");

return;

}

document.getElementById("result").value="";

for (var i=0;i<document.getElementById("content").value.length;i++){

result.value+="&#"+document.getElementById("content").value.charCodeAt(i)+";";

}

document.getElementById("content").focus();

}

//Unicode 转换 ASCII

function UnicodeToAscii(){

var code=document.getElementById("content").value.match(/&#(\d+);/g);

if (code==null){

alert("文本框中没有合法的Unicode代码!");

document.getElementById("content").focus();

return;

}

document.getElementById("result").value="";

for (var i=0;i<code.length;i++){

document.getElementById("result").value+=String.fromCharCode(code[i].replace(/[&#;]/g,""));

}

document.getElementById("content").focus();

}

function preview(){

var win=window.open();

win.document.open("text/html","replace");

win.document.writeln(document.getElementById("result").value);

win.document.close();

}

</script>

</head>

<body>

<table width="100%" border="0" cellpadding="0" cellspacing="1">

<tr>

<td height="50" colspan="2"><h1>Unicode编码转换工具</h1></td>

</tr>

<tr>

<td height="25" colspan="2">Unicode 转换 ASCII,ASCII 转换 Unicode</td>

</tr>

<tr>

<td width="50%"><textarea id="content" name="content" rows="20" cols="60"></textarea></td>

<td width="50%"><textarea id="result" name="result" rows="20" cols="60"></textarea></td>

</tr>

<tr>

<td height="30"><input name="button" type="button" value="ASCII 转换 Unicode↓"/>

<input name="button" type="button" value="Unicode 转换 ASCII↓"/>

<input name="button" type="button" onclick="result.value=''" value="清空结果" /></td>

<td height="30"><input name="button" type="button" value="预览转换代码" /></td>

</tr>

</table>

</body>

</html>

参考:http://tool.chinaz.com/Tools/Unicode.aspx

红色:http://zhidao.baidu.com/question/289602652.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息