Home > Web Front-end > JS Tutorial > body text

Detailed explanation of unicode transcoding method in js_javascript skills

WBOY
Release: 2016-05-16 15:36:57
Original
1717 people have browsed it

有时候遇到unicode不得不转码,我们只好人工编码进行转码。昨天在网上看到一个unitcode转码的方法,非常好用!小编把它和大家分享一下。
JavaScript脚本UniCode转码函数:

<script type="text/javascript">
   var GB2312UnicodeConverter = { 
    ToUnicode: function (str) {
      return escape(str).toLocaleLowerCase().replace(/%u/gi, '\\u');
      } 
    , ToGB2312: function (str) {
        return unescape(str.replace(/\\u/gi, '%u'));
      } 
  };
    var str = '上海', unicode;
  document.write(str + '<br/>');
  unicode = GB2312UnicodeConverter.ToUnicode(str);
  document.write('汉字转换为Unicode代码:' + unicode + '<br/><br/>');
  document.write('Unicode代码转换为汉字:' + GB2312UnicodeConverter.ToGB2312(unicode));  
</script>
Copy after login

这是一个写得非常不错的转码工具,大家可以把它整理成一个常用编程工具,需要的时候直接拿来使用!

Related labels:
js
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!