Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the ASCII value in JavaScript for the characters [duplicate]

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?

like image 957
Anand Murugan Avatar asked Jun 04 '12 09:06

Anand Murugan


People also ask

Which function in JavaScript finds the ASCII value of a specific element in an array?

In the above program, the charCodeAt() method is used to find the ASCII value of a character.

How do I get the ASCII code for 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.

How do I use charCodeAt in JavaScript?

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.

What returns the ASCII value of a given character?

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.


2 Answers

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.

like image 194
ioseb Avatar answered Sep 24 '22 01:09

ioseb


you can try

"str".charCodeAt(0) 
like image 37
Nikson Kanti Paul Avatar answered Sep 25 '22 01:09

Nikson Kanti Paul