Analysis of Chinese Characters and Unicode Code Mutual Transformation Function by JS Implementation

As used herein describes examples of Unicode characters and JS achieve mutual conversion function. Share to you for your reference, as follows:

Sometimes, we have passed the rear end of characters in a variable value, it may be due to coding, to the backend after the garbled. In order to save time or so sometimes other special requirements, it will pass into kanji conversion and then passed Unicode encoding.

Of course, it converted to a unicode characters encoded using JS, the charCodeAt () The method can.

  'good' .charCodeAt (0) .toString (16) "597d"  

This code means that the character ‘good’ converted to Unicode encoding, toString () is converted into a hexadecimal character to the

usage: charCodeAt () the method may Unicode encoding returns the position of the character. The return value is 0 – integer between 65535

Syntax: stringObject.charCodeAt (index)

index parameters required, a number representing the position of the string, i.e., in the character subscript string.

Note: of the first character string subscript is zero. If the index is negative, it is greater than or equal to the length of the string, the charCodeAt () Back NaN.

For example:

  var str = "Hello world!" Document.write (str.charCodeAt (1)) // Result: 101 'Ay '.charCodeAt (0) .toString (16) "597d"' Ay '.charCodeAt (1) .toString (16) "54e6"  

If trying to decode unicode characters?

in order to decode Unicode, it must use the escape character ‘\ u’

  '\ u54e6' "Oh,"  
under

summary:

js unicode hex code is applied at the beginning of \ u string representation. That \ unnnn

Unicode is to address the limitations of traditional character encoding scheme generated, it set a unified and unique binary encoding for each language for each character, to meet the cross-language, cross-platform text conversion processing requirements. 1990 R & D in 1994 officially announced.

A simple look at the following example, converted to unicode characters method:

  function toUnicodeFun (data) {if (data == '' || typeof data == 'undefined') return 'enter Character'; var str = ''; for (var i = 0; i <data.length;i++){
   str+="\\u"+data.charCodeAt(i).toString(16);
  }
  return str;
}
var resultUnicode = toUnicodeFun('中国'); // \u4e2d\u56fd
console.log(resultUnicode);

 

unicode to kanji conversion method :

  function toChineseWords (data) {if (data == '' || typeof data == 'undefined') return 'enter hexadecimal 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;
}
var resultChineseWords = toChineseWords("\u4e2d\u56fd");
console.log(resultChineseWords);//中国

 
Find another implementation:

 VAR GB2312UNICODECONVERTER = {Tounicode: function (str) {Return Escape (STR) .tolocalelowercase (). Replace (/% u / gi, '\\ u');}, TOGB2312: FUNCTION (STR) {Return Unescape (Str.Replace (/ \\ U / GI, '% u')); }}; var result = GB2312UNICODECONVERTER.TOUNICODE ('China'); // \ U4E2D \ u56fdvar result2 = GB2312UNICODECONVERTER.TOUNICODE (Result); //% 5CU4E2D% 5CU56FD   
】 The following implementation of the Chinese character to Unicode code:

Function Tounicode (s) {RETURN S.REPLACE (/ ([\ U4E00- \ U9fa5] | [\ ufe30- \ uffa0 ]) / g, function (newstr) {return “\\ u” + newstr.charcodeat (0) .tostring (16);};}
   
Ps: Here you will provide you with several Unicode encoded conversion operation related tools for your reference:

Online Unicode / Chinese Conversion Tool:

http: // Tools.jb51.net/transcoding / unicode_chinese

Native / Unicode online encoded conversion tool:
http://tools.jb51.net/transcoding/native2Unicode

Online Chinese Chinese characters / ASCII code / Unicode encoding mutual conversion tools:
http://tools.jb51.net/transcoding/chinese2Unicode

More about JavaScript Related Contents View this site: “JavaScript Coding operation skills summary 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