JS Implementation of Chinese Characters and Unicode Code Mutual Conversion

The example of this paper describes the method of JS to transition to the Unicode code to achieve Chinese characters. Share to everyone for reference, specifically:

js file, some variables may contain Chinese characters, after the picture introduces JS, it is possible to turn the Chinese characters in the character set. Garblered. Later, some JSs on the Internet will represent the Chinese characters in the variables into 16-based coding of “\ u”, which should be able to solve the above problems.

Recently, there were time to find a way of implementation, a relatively popular:

  Function Tounicode (data) {if (Data == ' ') Return' Please enter Chinese characters'; var str = ''; for (var i = 0; i <data.length;i++)
  {
   str+="\\u"+parseInt(data[i].charCodeAt(0),10).toString(16);
  }
  return str;
}
function tohanzi(data)
{
  if(data == '') return '请输入十六进制unicode';
  data = data.split("\u");
  var str ='';
  for(var i=0;i<data.length;i++)
  {
    str+=String.fromCharCode(parseInt(data[i],16).toString(10));
  }
  return str;
}

 

also found a relatively simple, but compare alternative:

VAR GB2312UNICODECONVERTER = {TOUNICODE: Function (STR) {Return Escape (STR) .tolocalelowercase (). Replace (/% u / gi, ‘\\ u’) }, TOGB2312: Function (STR) {RETURN UNESCAPE (Str.Replace (/ \\ U / GI, ‘% u’));}};
   
However Some problems, these two ways, all characters other than Chinese characters will be converted, do a simple encryption decision algorithm, but if you use to process JS files, turn your Enter, wrap, space, tab The characters are all changed. After the transfer, the JS file can not run.

I can’t write it off, I can only write one by the above code processing logic, as long as it guarantees that only the Chinese characters can be used:

// Chinese characters turn to Unicode character code representing function Tounicode (s) {return s.replace (/ ([\ u4e00- \ u9fa5] | [\ ufe30- \ uffa0] ) / g, function () {return “\\ u” + regexp [“$ 1”]. Charcodeat (0) .tostring (16);});}
   The method is written, in order to facilitate the contents of the JS file, make a simple page, add a button on the screen. First, in the JS file CTR + A, CTR + C, copy the content into the clipboard, and then create this screen, click Button, read the content from the clipboard, call the method to turn , Put the content back the clipboard. Then then CTR + A in the JS file, CTR + V is OK. The code is as follows: 

Function window_load () {var g = document.getlementByID; g (“cmdtou”). OnClick = function ( ) {ClipboardData.SetData (“text”, Tounicode (“text”)));}}} // Chinese characters to Unicode character code indicate // The original function is wrong, causing multiple Chinese As the result is the unicode code for the last Chinese character; Function Tounicode (s) {Return S.Replace (/ ([\ U4E00- \ U9FA5] | [\ ufe30- \ uffa0] / g, fUNCTION () {RETURN “\\ u” + regexp [“$ 1”]. Charcodeat (0) .tostring (16);});} // The Function Tounicode (s) {Return S is now changed by @ b4b4. Replace (/ ([\ U4E00- \ U9FA5] | [\ ufe30- \ uffa0]) / g, function (newstr) {return “\\ u” + newstr.charcodeat (0) .tostring (16);}); }
 Chinese characters to Unicode      This page can only be viewed in the IE kernel Under the machine, you can run it normally because the ClipboardData object seems to be only under IE.  PS: Here you will provide you with several Unicode encoded transformations related tools for your reference:    Online Unicode / Chinese Conversion Tool:   http://tools.jb51.net/transcoding/unicode_chinese 
Native / Unicode online encoding conversion tool:

http: tools.jb51.net/ Transcoding / native2Unicode

Online Chinese Characters / ASCII code / Unicode encoding mutual conversion tool:

http://tools.jb51.net/transcoding/chinese2Unicode
More about JavaScript Related Contents View this site: “JavaScriptCoding operation skills summary “,” JavaScript Encryption Decryption Skills “,” JavaScript Error and Debugging Skill Summary “,” JavaScript Data Structure and Algorithm Skill Summary “,” JavaScript Overclony Algorithm and Skill Summary “and” Summary of JavaScript Mathematical Computing Law ”
I hope this article will help you JavaScript programming.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment