51

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?

4

2 回答 2

105

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.

于 2012-06-04T09:42:45.353 回答
2

you can try

"str".charCodeAt(0)
于 2012-06-04T09:43:48.443 回答