Home  >  Article  >  Web Front-end  >  A brief discussion on character encoding conversion issues in JavaScript_Basic knowledge

A brief discussion on character encoding conversion issues in JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 15:51:031276browse

To get the Unicode encoding of a character, you can use the string.charCodeAt(index) method, which is defined as:

  strObj.charCodeAt(index)


Index is the position of the specified character in the strObj object (0-based index), and the return value is a 16-bit integer between 0 and 65535. For example:

   var strObj = "ABCDEFG";


   var code = strObj.charCodeAt(2); // Unicode value of character 'C' is 67


If there is no character at the index specified by index, the return value is NaN.

To convert Unicode encoding to a character, use the String.fromCharCode() method. Note that it is a "static method" of the String object, which means that you do not need to create a string instance before use:


 

  String.fromCharCode(c1, c2, ...)


It accepts 0 or more integers and returns a string containing the characters specified by each parameter, for example:


       

var str = String.fromCharCode(72, 101, 108, 108, 111); // str == "Hello"


Discussion:


Unicode contains character sets for many of the world’s written languages, but just because Unicode contains a character, don’t expect that character to display properly when an alert dialog box, text box, or page is rendered. If the character set is not available, it will appear on the page as a question mark or other symbol. A typical North American computer will not be able to display Chinese characters on the screen unless the Chinese character set and its fonts are installed.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn