Home  >  Article  >  Web Front-end  >  How to convert letters to numbers in javascript

How to convert letters to numbers in javascript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-20 13:31:047192browse

In JavaScript, you can use the charCodeAt() method to convert letters into numbers. The syntax format is "String object.charCodeAt (the subscript of the character in the string)".

How to convert letters to numbers in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Method: Convert characters to ascii code: use charCodeAt();

Convert ascii code to characters: use fromCharCode();

Example:

Convert letters Convert numbers into numbers:

var str='A'; str.charCodeAt()

Convert numbers into letters:

var num=65 ; String.fromCharCode(num);
获取小写字母a的代码:
a===>String.fromCharCode(0x60+1);

z===>String.fromCharCode(0x60+26)

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to convert letters to numbers in javascript. For more information, please follow other related articles on the PHP Chinese website!

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