{"id":1443,"date":"2008-10-09T21:44:44","date_gmt":"2008-10-09T13:44:44","guid":{"rendered":"https:\/\/pinyin.info\/news\/?p=1443"},"modified":"2015-11-27T22:29:25","modified_gmt":"2015-11-27T14:29:25","slug":"convert-chinese-characters-to-unicode-character-references-javascript","status":"publish","type":"post","link":"https:\/\/pinyin.info\/news\/2008\/convert-chinese-characters-to-unicode-character-references-javascript\/","title":{"rendered":"convert Chinese characters to Unicode character references: javascript"},"content":{"rendered":"<p>I&#8217;ve had a spate of requests recently for the code for Pinyin.info&#8217;s tool that <a href=\"https:\/\/pinyin.info\/tools\/converter\/chars2uninumbers.html\">converts Chinese characters to Unicode numeric character references<\/a> (i.e., something that converts, say, &#8220;&#28450;&#35486;&#25340;&#38899;&#8221; into &#8220;&amp;#28450;&amp;#35486;&amp;#25340;&amp;#38899;&#8221;). Since I&#8217;m a believer in open-source work &#8212; and since people could find the code anyway if they look carefully enough in the Web page&#8217;s source code &#8212; I might as well publish it. <\/p>\n<p>This tool can be very handy when making Web pages that use a variety of scripts. (It works on Cyrillic, etc., as well.) I often employ it myself. <\/p>\n<p>Here&#8217;s the heart of the code: <\/p>\n<blockquote><p><code><br \/>\nfunction convertToEntities() {<br \/>\n  var tstr = document.form.unicode.value;<br \/>\n  var bstr = '';<br \/>\n  for(i=0; i&lt;tstr.length; i++)<br \/>\n  {<br \/>\n    if(tstr.charCodeAt(i)&gt;127)<br \/>\n    {<br \/>\n      bstr += &#x0027;&#&#x0027; + tstr.charCodeAt(i) + &#x0027;;&#x0027;;<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n      bstr += tstr.charAt(i);<br \/>\n    }<br \/>\n  }<br \/>\n  document.form.entity.value = bstr;<br \/>\n}<\/code><\/p><\/blockquote>\n<p>This sleek little bit of Javascript is originally by <a href=\"http:\/\/minutillo.com\/steve\/\">Steve Minutillo<\/a> and used here on Pinyin.info with his permission. I may have tweaked the code a little myself; but that was so long ago I don&#8217;t remember well. (I&#8217;ve had the converter here for about five years.) Anyway, if you use this please acknowledge Steve&#8217;s authorship; and of course I always greatly appreciate links back to Pinyin.info. <\/p>\n<p>If anyone knows how to do the same thing in PHP &#8212; preferably with no more code than used above, please let me know. <\/p>\n<p>See also: <a href=\"https:\/\/pinyin.info\/news\/2008\/separating-pinyin-syllables-php-code\/\">separating Pinyin syllables: PHP code<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve had a spate of requests recently for the code for Pinyin.info&#8217;s tool that converts Chinese characters to Unicode numeric character references (i.e., something that converts, say, &#8220;&#28450;&#35486;&#25340;&#38899;&#8221; into &#8220;&amp;#28450;&amp;#35486;&amp;#25340;&amp;#38899;&#8221;). Since I&#8217;m a believer in open-source work &#8212; and since &hellip; <a href=\"https:\/\/pinyin.info\/news\/2008\/convert-chinese-characters-to-unicode-character-references-javascript\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,113,38,594],"tags":[601],"class_list":["post-1443","post","type-post","status-publish","format-standard","hentry","category-chinese-characters","category-computers","category-software","category-unicode-writing-systems","tag-javascript"],"_links":{"self":[{"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/posts\/1443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/comments?post=1443"}],"version-history":[{"count":25,"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/posts\/1443\/revisions"}],"predecessor-version":[{"id":6830,"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/posts\/1443\/revisions\/6830"}],"wp:attachment":[{"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/media?parent=1443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/categories?post=1443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/tags?post=1443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}