How to reverse String.fromCharCode?

String.fromCharCode(72) gives H. How to get number 72 from char H?

  • Is it possible to separate “\t” to char '\' + 't'
  • What's the best replacement for a char?
  • What is the 'char' keyword used for?
  • Char to Hex in javascript
  • Check First Char In String
  • Can we convert Unicode to ASCII in Javascript? charCodeAt() is only for Unicode?
  • Char to Hex in javascript
  • Can we convert Unicode to ASCII in Javascript? charCodeAt() is only for Unicode?
  • Is it possible to separate “\t” to char '\' + 't'
  • How to get the ASCII value in JavaScript for the characters
  • charCodeAt and fromCharCode do not return the same character
  • What's the best replacement for a char?
  • 4 Solutions collect form web for “How to reverse String.fromCharCode?”

    'H'.charCodeAt(0)
    

    Use charCodeAt:

    var str = 'H';
    var charcode = str.charCodeAt(0);
    

    Or Better yet:

    function CHR(ord)
    {
    return String.fromCharCode(ord);
    }
    
    function ORD(chr)
    {
    return chr.charCodeAt(0);
    }
    

    then using

    var mySTR = ORD(72);
    

    if you want to use them more than once, and/or a lot.

    @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)