📜  JavaScript程序查找字符的ASCII值

📅  最后修改于: 2020-09-27 04:48:21             🧑  作者: Mango

在此示例中,您将学习编写一个JavaScript程序来查找一个字符的ASCII值。

ASCII代表美国信息交换标准码

ASCII是一个数字值,赋予计算机存储和操作的不同字符和符号。例如,字母“ A”的ASCII值为65

资源 :JavaScript中所有127个字符的 ASCII图表。


实施例1: 字符使用charCodeAt的ASCII值()
// program to find the ASCII value of a character

// take input from the user
let string = prompt('Enter a character: ');

// convert into ASCII value
let result = string.charCodeAt(0);

console.log(`The ASCII value is: ${result}`);

输出

Enter a character: a
The ASCII value is: 97

在上述程序中, charCodeAt()方法用于查找一个字符的ASCII值。

charCodeAt()方法采用一个索引值,并返回一个表示其UTF-16(16位Unicode转换格式)代码的整数。

如果不传递索引值,则默认索引值为0 。如果索引值超出范围,则给出NaN


实施例2: 字符使用提供codePointAt的ASCII值()
// program to find the ASCII value of a character

// take input from the user
let string = prompt('Enter a character: ');

// convert into ASCII value
let result = string.codePointAt(0);

console.log(`The ASCII value is: ${result}`);

输出

Enter a character: abc
The ASCII value is: 97

在上面的程序中, codePointAt()方法用于查找一个字符的ASCII值。

codePointAt()方法返回Unicode代码点值。

在上面的程序中,用户输入三个字符的字符串 abc 。但是,索引0传递给codePointAt()方法。这给出了第一个字符的ASCII值(这里是a )。

如果不传递索引值,则默认索引值为0 。如果索引值超出范围,则给出undefined