javascripttypescriptunicodecharacter

How to convert a string (HTML Entity) to a unicode character


I have a string like

const mystring1 = "😁" // -> 😉
const mystring2 = "ሴ"  // -> ሴ
const mystring3 = "@"    // -> @

How do I get the Unicode characters using String.fromCodepoint()?


Solution

  • If you want to turn something like "😁" into the actual emoji using String.fromCodePoint(), just grab the hex part, convert it, and you're good.

    Here's what worked for me:

    parseInt(hex, 16) converts it from hex to decimal, and String.fromCodePoint gives you the actual character.

    function decodeHexEntity(hexEntity) {
      const hex = hexEntity.replace('&#x', '').replace(';', '');
      return String.fromCodePoint(parseInt(hex, 16));
    }
    
    const mystring1 = "😁";
    const mystring2 = "ሴ";
    const mystring3 = "@";
    
    console.log(decodeHexEntity(mystring1)); // 😁
    console.log(decodeHexEntity(mystring2)); // ሴ
    console.log(decodeHexEntity(mystring3)); // @