架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 12901|回复: 1

[JavaScript] Js汉字和Unicode编码互转 Unicode加密 Unicode解密

[复制链接]
发表于 2015-9-7 10:40:05 | 显示全部楼层 |阅读模式
Js实现汉字和Unicode编码互转,可实现Unicode加密、Unicode解密,操作时候,把需要转换的文字内容粘贴到文本框内,点击下边的对应按钮即可。你可保存本代码为一个HTML网页上传到网站中,这样你的网站就可以对外提供文字与Unicode编码转换的功能了。

  1. <title>汉字和Unicode编码互转</title>
  2. <scrip{过滤}t Language=Javascrip{过滤}t>
  3. var classObj=
  4.      {
  5.        ToUnicode:function(str)
  6.        {
  7.         return escape(str).replace(/%/g,"\").toLowerCase();
  8.        },
  9.        UnUnicode:function(str)
  10.        {
  11.         return unescape(str.replace(/\\/g, "%"));
  12.        },
  13.       copyingTxt:function(str)
  14.       {
  15.        document.getElementById(str).select();
  16.        document.execCommand("Copy");
  17.       }
  18.     }
  19. </scrip{过滤}t>
  20. <textarea id=codes style="width:500px;height:300px"></textarea><br><br>
  21. <input type=button value=Unicode加密 onclick=javascrip{过滤}t:codes.value=classObj.ToUnicode(codes.value)>
  22. <input type=button value=Unicode解密 onclick=javascrip{过滤}t:codes.value=classObj.UnUnicode(codes.value)>
  23. <input type=button value=复制文本 onclick=javascrip{过滤}t:classObj.copyingTxt("codes")>
  24. <input type=button value=清空内容 onclick=javascrip{过滤}t:codes.value="">
复制代码






上一篇:与君共勉
下一篇:c#版Unicode字符集批量转码为汉字(互转)
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2015-9-7 11:22:38 | 显示全部楼层
上述方法解码数字会出现错误

QQ截图20150907111515.jpg

\u9519\u8bef\u8d85\u8fc7\u35\u6b21\uff0c\u5173\u95ed\u6d4f\u89c8\u5668\u91cd\u65b0\u767b\u5f55\uff01
用以下的方法,不会出现此类问题:

  1. function reconvert(str){
  2. str = str.replace(/(\\u)(\w{1,4})/gi,function($0){
  3. return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{1,4})/g,"$2")),16)));
  4. });
  5. str = str.replace(/(&#x)(\w{1,4});/gi,function($0){
  6. return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{1,4})(%3B)/g,"$2"),16));
  7. });
  8. str = str.replace(/(&#)(\d{1,6});/gi,function($0){
  9. return String.fromCharCode(parseInt(escape($0).replace(/(%26%23)(\d{1,6})(%3B)/g,"$2")));
  10. });
  11. return str;
  12. }
复制代码


码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-9-18 02:50

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表