文字編碼轉換[待補充]

<!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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章