Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 15:51:03
Original
1326 people have browsed it

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

  strObj.charCodeAt(index)
Copy after login


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

Copy after login


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, ...)
Copy after login


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"
Copy after login


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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template