Announcement

Collapse
No announcement yet.

how can I get the ASCII value of a character?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • how can I get the ASCII value of a character?

    Hi

    I have a string variable, e.g.
    var s = "a string";

    I wish to step through each character of the string, and carry out a computation on the ASCII value of the character. If I do the following:

    for(var i=0; i<s.length; i++)
    {
    var n = s.charAt(i);
    }
    Then n correctly receives each character of the string, but unfortunately, I am unable to carry out any computations on n becase, I presume, it too is a string object.

    If I was writing in 'C', I would put:

    int n = (int)s[i];

    How can I convert a character from a string into its numeric ASCII value?

    Thanks

  • #2
    Sure. just use the charCodeAt() method.

    var x = "A";

    var asciiVal = x.charCodeAt(0);

    Chris

    Comment


    • #3
      Many thanks - that's just what I was after. Is this info in a help file anywhere?

      Comment


      • #4
        trubshac
        You can find it in the Core JavaScript Guide which is in the EFS KnowledgeBase (see this article on String functions and methods)
        Alex

        Comment

        Working...
        X