Home  >  Article  >  Web Front-end  >  How to convert unicode to ASCII in javascript_javascript skills

How to convert unicode to ASCII in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:26:391865browse

The example in this article describes the method of converting unicode to ASCII in JavaScript. Share it with everyone for your reference, the details are as follows:

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Unicode、ASCII相互转换</title>
<script type="text/javascript">
//AsciiToUnicode("中国");
UnicodeToAscii("&#27979;&#35797;");
//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>

I hope this article will be helpful to everyone in JavaScript programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn