如何在 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
How to get the ASCII value in JavaScript for the characters
提问by Anand Murugan
Possible Duplicate:
Convert character to ASCII code in Javascript
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)