How can I convert a character to its ASCII code using JavaScript?
For example:
get 10 from "\n".
How can I convert a character to its ASCII code using JavaScript? For example:
|
||||
|
|||||||||||||||||||||
|
For opposite use
Here is a quick ASCII characters reference:
|
|||||||||||||||||
|
If you have only one char and not a string, you can use:
omitting the 0... |
|||||
|
While the other answers are right, I prefer this way:
Then, to use it, simply:
I am using this for a small shortcut system:
And you can even use it inside map() or other methods:
|
|||||||||||||
|
For those that want to get a sum of all the ASCII codes for a string:
Or, ES6:
|
|||||||||||||||||
|
For supporting all UTF-16 (also non-BMP/supplementary characters) from ES6 the string.codePointAt() method is available; This method is an improved version of charCodeAt which could support only unicode codepoints < 65536 ( 216 - a single 16bit ) . |
|||
|
I found this open source function inside the javascript-algorithms library:
|
||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
"Hello, World"
or'Hello, World'
, same with characters. – wizzwizz4 Jan 13 at 19:06