javascript 实现 Unicode、ASCII相互转换!

javascript 实现 Unicode、ASCII相互转换!有时会用到。 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Unicode、ASCII相互转换</title>
<script type="text/javascript">
AsciiToUnicode("中国");
UnicodeToAscii("测试");
//ASCII 转换 Unicode
function AsciiToUnicode(content) {
	result = '';
	for (var i=0; i<content.length; i++)
	result+='&#' + content.charCodeAt(i) + ';';
	alert("ASCII:"+content+"\nUnicode:"+result);
}
//Unicode 转换 ASCII
function UnicodeToAscii(content) { 
	var code = content.match(/&#(\d+);/g);
	result= '';
	for (var i=0; i<code.length; i++)
	result += String.fromCharCode(code[i].replace(/[&#;]/g, ''));
	alert("Unicode:"+content+"\nASCII:"+result);
}
</script>
</head>
<body>
</body>
</html>

你可能感兴趣的:(JavaScript,测试)