javascript:中文等字符转成unicode

复制代码
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 
 4 <head>
 5 <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
 6 <title>chinese word convert unicode using Java/JavaScript String Literals :IE 9+ 塘&#13901;镇 windowsXP 对一些比较少用的生僻字难输入显示 Window 7好些  </title>
 7 </head>
 8 
 9 
10 
11 <body>
12 <h3 class="fz14b mb" id="xzqh">
13                 行政区划</h3>
14             <div class="a_adm">
15                 <ul>
16                     <li>
17                         吴川市辖5个街道(梅菉、博铺、海滨、塘尾、大山江),10个镇(兰石镇、覃巴镇、吴阳镇、黄坡镇、振文镇、樟铺镇、塘&#13901;镇、长岐镇、浅水镇、王村港镇)。</li>
18                     <li>
19                         联系方式:塘 <script type="text/javascript" charset="utf-8">document.write(unescape('\u364D'));</script><script type="text/javascript" charset="utf-8">document.write(unescape('%u20AC \xDCbergr\xF6\xDFe  \u5858\u364D\u9547'));</script> </li>
20                     <li>
21                         区号:0759 塘&#13901;镇(&#13901;字为“土、叕”两字合为一字IE9+ 以上显示正常。否则要安装字库才为正常显示)&#22615;&#32858;&#25991;</li>
22                     <li>
23                         邮编:440883 Unicode编码:
24     十进制:13901
25     UTF-8:E3 99 8D 
26     UTF-16:364D
27     UTF-32:0000364D
28 
29 </li>
30                 </ul>
31             </div>
32             
33             <input id="i0" name="lang" onclick="refresh()" type="radio" checked/><label for="i0" title="\u00FF only">Java String Encoder</label><br/>
34 <input id="i1" name="lang" onclick="refresh()" type="radio"/><label for="i1" title="\xFF and \u0100">JavaScript String Encoder</label>
35 <textarea onkeyup="refresh()" rows="8" cols="100">// Paste some Java or JavaScript code into this window.
36 german = "&Uuml;bergr&ouml;&szlig;e 塘&#13901;镇";
37 smilie = "&#x263A;";</textarea>
38 <pre onclick="select(this)">This encoding utility requires JavaScript.</pre>
39 <script type="text/javascript">
40 //from: http://maettig.com/code/javascript/encode-javascript-string-in-140byt.es.html
41 //http://www.unicodetools.com/unicode/convert-to-html.php
42 //http://zh.wiktionary.org/zh/%E3%99%8D
43 // Version history:
44 //function(a){return a.replace(/[^ -~]/g,function(b){b=b.charCodeAt(0);return'\\'+(b<256?'x':b<4096?'u0':'u')+b.toString(16).toUpperCase()})}
45 //function f(a,b){return++b?'\\'+((a=a.charCodeAt(0))>>12?'u':a>>8?'u0':'x')+a.toString(16).toUpperCase():a.replace(/[^ -~]/g,f)}
46 //function(a){return a.replace(/[^ -~]/g,function(b){b=b.charCodeAt(0);return'\\'+(b>>12?'u':b>>8?'u0':'x')+b.toString(16).toUpperCase()})}
47 //function(a){return a.replace(/[^ -~]/g,function(b){return'\\'+((b=b.charCodeAt(0))>>12?'u':b>>8?'u0':'x')+b.toString(16).toUpperCase()})}
48 //function f(a,b){return b>=0?'\\'+((a=a.charCodeAt(0))<256?'x':a<4096?'u0':'u')+a.toString(16).toUpperCase():a.replace(/[^ -~]/g,f)}
49 //function f(a,b){return++b?'\\'+((a=a.charCodeAt(0))<256?'x':a<4096?'u0':'u')+a.toString(16).toUpperCase():a.replace(/[^\0-~]/g,f)}
50 //function g(a,b){return++b?'\\'+((a=a.charCodeAt(0))>>12?'u':a>>8?'u0':'x')+a.toString(16).toUpperCase():a.replace(/[^\0-~]/g,g)}
51 //function g(a,b){return++b?'\\'+((a=a.charCodeAt())>>12?'u':a>>8?'u0':'x')+a.toString(16).toUpperCase():a.replace(/[^\0-~]/g,g)}
52 
53 //function f(a,b){return++b?'\\u'+((a=a.charCodeAt(0))>>12?'':a>>8?'0':'00')+a.toString(16).toUpperCase():a.replace(/[^ -~]/g,f)}
54 //function f(a,b){return++b?'\\u'+(65536|a.charCodeAt(0)).toString(16).slice(-4).toUpperCase():a.replace(/[^ -~]/g,f)}
55 //function f(a,b){return++b?'\\u'+('00'+a.charCodeAt(0).toString(16)).slice(-4).toUpperCase():a.replace(/[^\0-~]/g,f)}
56 //function f(a,b){return++b?'\\u'+('00'+a.charCodeAt().toString(16)).slice(-4).toUpperCase():a.replace(/[^\0-~]/g,f)}
57 
58 // 127 bytes
59 var encodeJavaScriptString = function f(a, b)
60 {
61   return ++b                                 //`b` is a number (including 0) when `replace` calls the function
62     ? '\\' + (                               //all escape sequences start with a backslash
63       (a = a.charCodeAt()) >> 12             //all characters from U+1000 and above
64         ? 'u'                                //must start with `\u`
65         : a >> 8                             //all characters from U+0100 to U+0FFF
66           ? 'u0'                             //must start with `\u0`
67           : 'x'                              //characters from U+007F to U+00FF can start with `\u00` or `\x`
68       ) + a.toString(16).toUpperCase()       //add the upper case hex string (it does not contain leading zeros)
69     : a.replace(/[^\0-~]/g, f)               //else call the function for all non-ASCII characters (all except U+0000 to U+007E)
70 }
71 // 115 bytes
72 var encodeJavaString = function e(a, b)
73 {
74   return ++b                                 //`b` is a number when `replace` calls the function
75     ? '\\u' +                                //in Java all escape sequences must start with `\u`
76       ('00' + a.charCodeAt().toString(16))   //build a hex string with at least 4 characters
77       .slice(-4).toUpperCase()               //use the last 4 characters and make them upper case
78     : a.replace(/[^\0-~]/g, e)               //else call the function for all non-ASCII characters (all except U+0000 to U+007E)
79 }
80 // 89 bytes
81 var select = function(a, b)
82 {
83   b = document.createRange();
84   b.selectNode(a);
85   window.getSelection().addRange(b)
86 }
87 var refresh = function()
88 {
89   var t = document.getElementsByTagName('TEXTAREA')[0];
90   var p = document.getElementsByTagName('PRE')[0];
91   var f = document.getElementById('i1').checked ? encodeJavaScriptString : encodeJavaString;
92   p.firstChild.data = f(t.value).replace(/\r\n/g, '\n');
93 }
94 refresh();
95 </script>
96 </body>
97 
98 </html>
复制代码

 

posted @   ®Geovin Du Dream Park™  阅读(1314)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2010-06-18 Sql 脚本导入EXCEL数据
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示