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?
In the above program, the charCodeAt() method is used to find the ASCII value of a character.
Program to Print ASCII ValueThe character is stored in variable c . When %d format string is used, 71 (the ASCII value of G ) is displayed. When %c format string is used, 'G' itself is displayed.
The charCodeAt() method returns the Unicode of the character at a specified index (position) in a string. The index of the first character is 0, the second is 1, .... The index of the last character is string length - 1 (See Examples below). See also the charAt() method.
Here are few methods in different programming languages to print ASCII value of a given character : Python code using ord function : ord() : It converts the given string of length one, returns an integer representing the Unicode code point of the character. For example, ord('a') returns the integer 97.
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.
you can try
"str".charCodeAt(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With