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 xDCbergrxF6xDFe  u5858u364Du9547'));</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(/[^-~]/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(/[^-~]/g,g)}
51 //function g(a,b){return++b?'\'+((a=a.charCodeAt())>>12?'u':a>>8?'u0':'x')+a.toString(16).toUpperCase():a.replace(/[^-~]/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(/[^-~]/g,f)}
56 //function f(a,b){return++b?'\u'+('00'+a.charCodeAt().toString(16)).slice(-4).toUpperCase():a.replace(/[^-~]/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(/[^-~]/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(/[^-~]/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(/
/g, '
');
93 }
94 refresh();
95 </script>
96 </body>
97 
98 </html>
原文地址:https://www.cnblogs.com/geovindu/p/3142057.html