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

How to convert letters to numbers in javascript

醉折花枝作酒筹
Release: 2023-01-07 11:44:24
Original
7251 people have browsed it

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

Convert numbers into letters:

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

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

[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!

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