Home > Article > Web Front-end > How to convert javascript to ascii code
Conversion method: 1. Use the fromCharCode method to return a string according to the serial number value, the syntax "String.fromCharCode(num1, ..., numN)"; 2. Use the charCodeAt method to return an integer, the syntax "charCodeAt(index )".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Methods provided in JavaScript to convert ascii codes into corresponding letters, and convert corresponding letters into ascii codes:
String.fromCharCode()
Static method according to the specification Returns a string with an ordinal value in Unicode encoding.
Syntax:
String.fromCharCode(num1, ..., numN)
String.prototype.charCodeAt()
The method returns an integer between 0 and 65535, representing the UTF of the character at the index -16 code unit (In the case where the Unicode code unit represents a single UTF-16 code unit, the UTF-16 code unit matches the Unicode code unit. Otherwise, for example, in the case of Unicode code unit > 0x10000, only the Unicode proxy can be matched the first coding unit of the pair). If you want to get the whole point encoded value, use codePointAt().
Syntax:
str.charCodeAt(index)
index
An integer greater than or equal to 0 and less than the length of the string. If not a numeric value, defaults to 0.
"ABC".charCodeAt(0) // returns 65
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to convert javascript to ascii code. For more information, please follow other related articles on the PHP Chinese website!