<!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=utf-8" />
<title>文字编码转换</title>
<script type="text/javascript">
//转换成ASCII码
function ascii(str) {
return str.replace(/[^\u0000-\u00FF]/g, function($0) {
return escape($0).replace(/(%u)(\w{4})/gi, "\&#x$2;")
});
}
//转换成Unicode编码
function unicode(str) {
return str.replace(/[^\u0000-\u00FF]/g, function($0) {
return escape($0).replace(/(%u)(\w{4})/gi, "\\u$2")
});
}
//转换成中文
function reconvert(str) {
//Unicode
str = str.replace(/(\\u)(\w{4})/gi, function($0) {
return (String.fromCharCode(parseInt((escape($0).replace(
/(%5Cu)(\w{4})/g, "$2")), 16)));
});
//ASCII
str = str.replace(/(&#x)(\w{4});/gi, function($0) {
return String.fromCharCode(parseInt(escape($0).replace(
/(%26%23x)(\w{4})(%3B)/g, "$2"), 16));
});
return str;
}
//中文转换为Asc2
function chToAsc2() {
var content = document.getElementById("ctoas");
var asc2Value = ascii(content.value);
document.getElementById("ctoat").value = asc2Value;
}
//中文转换为Unicode
function chToUnicode() {
var content = document.getElementById("ctous");
var unicodeValue = unicode(content.value);
document.getElementById("ctout").value = unicodeValue;
}
//Unicode或Asc2反转为中文
function codeToCh() {
var content = document.getElementById("ctocs");
var chValue = reconvert(content.value);
document.getElementById("ctoct").value = chValue;
}
</script>
</head>
<body style="text-align: center;">
<hr></hr>
<!--中文转换成ASCII-->
<div id="chtoasc2">
<textarea id="ctoas" name="ctoas" rows="2" cols="20"></textarea>
<br /> <input type="button" value="中文→ASCII" οnclick="chToAsc2();" /><br />
<textarea id="ctoat" name="ctoat" rows="2" cols="20"></textarea>
<br />
</div>
<hr></hr>
<!--中文转换成Unicode-->
<div id="chtounicode">
<textarea id="ctous" name="ctous" rows="2" cols="20"></textarea>
<br /> <input type="button" value="中文→Unicode"
οnclick="chToUnicode();" /><br />
<textarea id="ctout" name="ctout" rows="2" cols="20"></textarea>
<br />
</div>
<hr></hr>
<!--Unicode或ASCII编码翻转为中文-->
<div id="ctoch">
<textarea id="ctocs" name="ctocs" rows="2" cols="20"></textarea>
<br /> <input type="button" value="反转为中文" οnclick="codeToCh();" /><br />
<textarea id="ctoct" name="ctoct" rows="2" cols="20"></textarea>
<br />
</div>
<hr></hr>
</body>
</html>
文字编码转换[待补充]
最新推荐文章于 2022-04-26 11:33:37 发布