This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13376|Reply: 1

[JavaScript] JS Chinese characters and Unicode encoding are exchanged Unicode encryption Unicode decryption

[Copy link]
Posted on 9/7/2015 10:40:05 AM | | | |
Js realizes the interconversion of Chinese characters and Unicode encoding, which can realize Unicode encryption and Unicode decryption. You can save this code as an HTML page and upload it to your website, so that your website can provide text and Unicode encoding conversion capabilities.

  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="">
Copy code






Previous:I will encourage you together
Next:Batch transcoding of c# version of Unicode character sets to Chinese characters (mutual conversion)
 Landlord| Posted on 9/7/2015 11:22:38 AM |
The above method will decode the numbers with errors



\u9519\u8bef\u8d85\u8fc7\u35\u6b21\uff0c\u5173\u95ed\u6d4f\u89c8\u5668\u91cd\u65b0\u767b\u5f55\uff01
With the following methods, such problems will not occur:

  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. }
Copy code


Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com