如何在 JavaScript 中获取字符的 ASCII 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10879244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:23:44  来源:igfitidea点击:

How to get the ASCII value in JavaScript for the characters

javascriptcharascii

提问by Anand Murugan

Possible Duplicate:
Convert character to ASCII code in Javascript

可能的重复:
在 Javascript 中将字符转换为 ASCII 代码

my requirement is to get the ASCII value of the alphabet letters... Can anyone suggest how to do this in JavaScript?

我的要求是获取字母的 ASCII 值......谁能建议如何在 JavaScript 中做到这一点?

回答by ioseb

Here is the example:

这是示例:

var charCode = "a".charCodeAt(0);
console.log(charCode);

Or if you have longer strings:

或者,如果您有更长的字符串:

var string = "Some string";

for (var i = 0; i < string.length; i++) {
  console.log(string.charCodeAt(i));
}

String.charCodeAt(x)method will return ASCII character code at a given position.

String.charCodeAt(x)方法将返回给定位置的 ASCII 字符代码。

回答by Nikson Kanti Paul

you can try

你可以试试

"str".charCodeAt(0)