What is an easy way to call Asc() and Chr() in JavaScript for Unicode values?

前端 未结 3 1476
小蘑菇
小蘑菇 2021-02-05 00:12

I am not that familiar with Javascript, and am looking for the function that returns the UNICODE value of a character, and given the UNICODE value, returns the string equivalent

3条回答
  •  花落未央
    2021-02-05 00:33

    Have a look at:

    String.fromCharCode(64)
    

    and

    String.charCodeAt(0)
    

    The first must be called on the String class (literally String.fromCharCode...) and will return "@" (for 64). The second should be run on a String instance (e.g., "@@@".charCodeAt...) and returns the Unicode code of the first character (the '0' is a position within the string, you can get the codes for other characters in the string by changing that to another number).

    The script snippet:

    document.write("Unicode for character ਔ is: " + "ਔ".charCodeAt(0) + "
    "); document.write("Character 2580 is " + String.fromCharCode(2580) + "
    ");

    gives:

    Unicode for character ਔ is: 2580
    Character 2580 is ਔ
    

提交回复
热议问题