Javascript 字元轉換成ASCII碼

請問我要如何使用Javascript 把\"a\"轉換成ASCII碼為\"97\"呢?

我找了許多資料只有看到用escape()把字串轉換成ASCII

但卻不支援英文字母和數字

請求高手幫忙

感謝!

2 Answers

Rating
  • 2 decades ago
    Favorite Answer

    沒有 ASCII 轉換函數,但是有 Uni-code 的,也可以適用

    charCodeAt :Returns an integer representing the Unicode encoding of the character at the specified location.

    語法 : strObj.charCodeAt(index)

    strObj : Required. Any String object or literal.

    index : Required. Zero-based index of the desired character. Valid values are between 0 and the length of the string minus 1.

    附註 : The first character in a string is at index 0, the second is at index 1, and so forth. If there is no character at the specified index, NaN is returned.

    範例 : The following example illustrates the use of the charCodeAt method.

    function charCodeAtTest(n){

    var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Initialize variable.

    var n; //Declare variable.

    n = str.charCodeAt(n - 1); //Get the Unicode value of the

    // character at position n.

    return(n); //Return the value.

    }

    2006-04-20 15:36:37 補充:

    U r right ! the var n should be declared as an another name. But the function usage is correct.

    Source(s): Microsoft
  • 2 decades ago

    傳進來的參數是 n 變數也是 n, 不會打架嗎?

Still have questions? Get your answers by asking now.