SoFunction
Updated on 2025-02-28

Analysis of the mutual conversion function of Chinese characters and Unicode codes implemented by JS

This article describes the mutual conversion function of Chinese characters and Unicode code implemented by JS. Share it for your reference, as follows:

Sometimes, we have Chinese characters in the value of the variable to the backend, but it may become garbled after passing to the backend due to encoding reasons. Therefore, sometimes in order to save trouble or other special requirements, the passed Chinese characters will be converted into Unicode encoding before passing them.

Of course, Chinese characters are converted into unicode encoding, using JScharCodeAt()Just the method.

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

This code means converting the character 'good' into Unicode encoding.toString()It's just that characters are converted into hexadecimal

usage:charCodeAt()Methods can return the Unicode encoding of characters at the specified location. This return value is an integer between 0 - 65535

grammar:(index)

indexThe parameter is required to represent a number at a certain position in the string, that is, the subscript of the character in the string.

Note:The subscript of the first character in the string is 0. If index is a negative number, or greater than or equal to the length of the string,charCodeAt()Return to NaN.

For example:

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

What if you want to decode unicode into characters?

To decode Unicode, you must use escaped characters '\u'

'\u54e6'
"oh"

To summarize:

js unicode is a string represented by hexadecimal code plus the beginning of \u. Right now\unnnn

Unicode is created to solve the limitations of traditional character encoding schemes. It sets a unified and unique binary encoding for each character in each language to meet the requirements of cross-language and cross-platform text conversion and processing. Research and development began in 1990 and officially announced in 1994.

Let’s first look at a simple example, converting Chinese characters into unicode methods:

function toUnicodeFun(data){
 if(data == '' || typeof data == 'undefined') return 'Please enter Chinese characters';
  var str ='';
  for(var i=0;i<;i++){
   str+="\\u"+(i).toString(16);
  }
  return str;
}
var resultUnicode = toUnicodeFun('China'); // \u4e2d\u56fd
(resultUnicode);

How to convert unicode into Chinese characters:

function toChineseWords(data){
  if(data == '' || typeof data == 'undefined') return 'Please enter hexadecimal unicode';
  data = ("\\u");
  var str ='';
  for(var i=0;i<;i++){
    str+=(parseInt(data[i],16).toString(10));
  }
  return str;
}
var resultChineseWords = toChineseWords("\u4e2d\u56fd");
(resultChineseWords);//China

Find another implementation method online:

var GB2312UnicodeConverter={
  ToUnicode:function(str){
    return escape(str).toLocaleLowerCase().replace(/%u/gi,'\\u');
  },
  ToGB2312:function(str){
    return unescape((/\\u/gi,'%u'));
  }
};
var result = ('China'); //\u4e2d\u56fd
var result2 = (result); //%5cu4e2d%5cu56fd

The following is to convert Chinese characters to Unicode code:

function toUnicode(s){
  return (/([\u4E00-\u9FA5]|[\uFE30-\uFFA0])/g,function(newStr){
    return "\\u" + (0).toString(16);
  });
}

PS: Here are a few Unicode encoding and conversion operations related tools for your reference:

Online Unicode/Chinese conversion tool:
http://tools./transcoding/unicode_chinese

Native/Unicode online encoding and conversion tool:
http://tools./transcoding/native2unicode

Online Chinese characters/ASCII code/Unicode encoding mutual conversion tool:
http://tools./transcoding/chinese2unicode

For more information about JavaScript, please view the special topic of this site: "Summary of JavaScript encoding operation skills》、《Summary of JavaScript encryption and decryption skills》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.