中文转换Ascii和Unicode编码小工具-代码

时间:2020-12-08 20:21:13

<html>
 <head>
 <script>
 function encode2ascii(s,t){
  t.value="";
  var str=s.value;
  t.value=str.replace(/[^/u0000-/u00FF]/g,function($0){return escape($0).replace(/(%u)(/w{4})/gi,"//u$2")});
 }
 

 function encode2unicode(s,t){
  t.value="";
  var str=s.value;
  t.value=str.replace(/[^/u0000-/u00FF]/g,function($0){return escape($0).replace(/(%u)(/w{4})/gi,"&#x$2;")});
 }
 </script>
 </head>
 <body>
  源码:
  <br>
  <textarea  rows=5 id="source" style="WIDTH: 70%"></textarea>
  <p>
  <input type="button" onclick="encode2ascii(source,target)" value="转化Ascii编码">

  <input type="button" onclick="encode2unicode(source,target)" value="转化Unicode编码">
  <p>
  转换结果:
  <br>
  <textarea rows=5 id=target style="WIDTH:70%"></textarea>
 </body>
</html>