且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

javascript:中文等字符转成unicode

更新时间:2022-08-14 15:46:38

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>chinese word convert unicode using Java/JavaScript String Literals :IE 9+ 塘㙍镇 windowsXP 对一些比较少用的生僻字难输入显示 Window 7好些  </title>
</head>



<body>
<h3 class="fz14b mb" id="xzqh">
				行政区划</h3>
			<div class="a_adm">
				<ul>
					<li>
						吴川市辖5个街道(梅菉、博铺、海滨、塘尾、大山江),10个镇(兰石镇、覃巴镇、吴阳镇、黄坡镇、振文镇、樟铺镇、塘㙍镇、长岐镇、浅水镇、王村港镇)。</li>
					<li>
						联系方式:塘 <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>
					<li>
						区号:0759 塘㙍镇(㙍字为“土、叕”两字合为一字IE9+ 以上显示正常。否则要安装字库才为正常显示)&#22615;&#32858;&#25991;</li>
					<li>
						邮编:440883 Unicode编码:
    十进制:13901
    UTF-8:E3 99 8D 
    UTF-16:364D
    UTF-32:0000364D

</li>
				</ul>
			</div>
			
			<input id="i0" name="lang" onclick="refresh()" type="radio" checked/><label for="i0" title="\u00FF only">Java String Encoder</label><br/>
<input id="i1" name="lang" onclick="refresh()" type="radio"/><label for="i1" title="\xFF and \u0100">JavaScript String Encoder</label>
<textarea onkeyup="refresh()" rows="8" cols="100">// Paste some Java or JavaScript code into this window.
german = "Übergröße 塘㙍镇";
smilie = "";</textarea>
<pre onclick="select(this)">This encoding utility requires JavaScript.</pre>
<script type="text/javascript">
//from: http://maettig.com/code/javascript/encode-javascript-string-in-140byt.es.html
//http://www.unicodetools.com/unicode/convert-to-html.php
//http://zh.wiktionary.org/zh/%E3%99%8D
// Version history:
//function(a){return a.replace(/[^ -~]/g,function(b){b=b.charCodeAt(0);return'\\'+(b<256?'x':b<4096?'u0':'u')+b.toString(16).toUpperCase()})}
//function f(a,b){return++b?'\\'+((a=a.charCodeAt(0))>>12?'u':a>>8?'u0':'x')+a.toString(16).toUpperCase():a.replace(/[^ -~]/g,f)}
//function(a){return a.replace(/[^ -~]/g,function(b){b=b.charCodeAt(0);return'\\'+(b>>12?'u':b>>8?'u0':'x')+b.toString(16).toUpperCase()})}
//function(a){return a.replace(/[^ -~]/g,function(b){return'\\'+((b=b.charCodeAt(0))>>12?'u':b>>8?'u0':'x')+b.toString(16).toUpperCase()})}
//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)}
//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)}
//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)}
//function g(a,b){return++b?'\\'+((a=a.charCodeAt())>>12?'u':a>>8?'u0':'x')+a.toString(16).toUpperCase():a.replace(/[^\0-~]/g,g)}

//function f(a,b){return++b?'\\u'+((a=a.charCodeAt(0))>>12?'':a>>8?'0':'00')+a.toString(16).toUpperCase():a.replace(/[^ -~]/g,f)}
//function f(a,b){return++b?'\\u'+(65536|a.charCodeAt(0)).toString(16).slice(-4).toUpperCase():a.replace(/[^ -~]/g,f)}
//function f(a,b){return++b?'\\u'+('00'+a.charCodeAt(0).toString(16)).slice(-4).toUpperCase():a.replace(/[^\0-~]/g,f)}
//function f(a,b){return++b?'\\u'+('00'+a.charCodeAt().toString(16)).slice(-4).toUpperCase():a.replace(/[^\0-~]/g,f)}

// 127 bytes
var encodeJavaScriptString = function f(a, b)
{
  return ++b                                 //`b` is a number (including 0) when `replace` calls the function
    ? '\\' + (                               //all escape sequences start with a backslash
      (a = a.charCodeAt()) >> 12             //all characters from U+1000 and above
        ? 'u'                                //must start with `\u`
        : a >> 8                             //all characters from U+0100 to U+0FFF
          ? 'u0'                             //must start with `\u0`
          : 'x'                              //characters from U+007F to U+00FF can start with `\u00` or `\x`
      ) + a.toString(16).toUpperCase()       //add the upper case hex string (it does not contain leading zeros)
    : a.replace(/[^\0-~]/g, f)               //else call the function for all non-ASCII characters (all except U+0000 to U+007E)
}
// 115 bytes
var encodeJavaString = function e(a, b)
{
  return ++b                                 //`b` is a number when `replace` calls the function
    ? '\\u' +                                //in Java all escape sequences must start with `\u`
      ('00' + a.charCodeAt().toString(16))   //build a hex string with at least 4 characters
      .slice(-4).toUpperCase()               //use the last 4 characters and make them upper case
    : a.replace(/[^\0-~]/g, e)               //else call the function for all non-ASCII characters (all except U+0000 to U+007E)
}
// 89 bytes
var select = function(a, b)
{
  b = document.createRange();
  b.selectNode(a);
  window.getSelection().addRange(b)
}
var refresh = function()
{
  var t = document.getElementsByTagName('TEXTAREA')[0];
  var p = document.getElementsByTagName('PRE')[0];
  var f = document.getElementById('i1').checked ? encodeJavaScriptString : encodeJavaString;
  p.firstChild.data = f(t.value).replace(/\r\n/g, '\n');
}
refresh();
</script>
</body>

</html>