How to reverse String.fromCharCode?
String.fromCharCode(72) gives H. How to get number 72 from char H?
Solution 1:
'H'.charCodeAt(0)
Solution 2:
Use charCodeAt:
var str = 'H';
var charcode = str.charCodeAt(0);
Solution 3:
@Silvio's answer is only true for code points up to 0xFFFF (which in the end is the maximum that String.fromCharCode can output). You can't always assume the length of a character is one:
'𐌰'.length
-> 2
Here's something that works:
var utf16ToDig = function(s) {
var length = s.length;
var index = -1;
var result = "";
var hex;
while (++index < length) {
hex = s.charCodeAt(index).toString(16).toUpperCase();
result += ('0000' + hex).slice(-4);
}
return parseInt(result, 16);
}
Using it:
utf16ToDig('𐌰').toString(16)
-> "d800df30"
(Inspiration from https://mothereff.in/utf-8)