How to use five digit long Unicode characters in JavaScript

前端 未结 5 2090

In JavaScript I can do this:

foo = \"\\u2669\" // 1/4 note

But I can\'t do this

foo = \"\\u1D15D\" // full note  -five hex digi         


        
5条回答
  •  遥遥无期
    2021-02-19 10:34

    You can use this:

    function fromOutsideBMP(cp) {
    // 0x01D120
      var x=cp-0x10000;
      var top10=parseInt("11111111110000000000",2);
      var end10=parseInt("1111111111",2);
      var part1=(x&top10)/1024+0xD800;
      var part2=(x&end10)+0xDC00;
      var s=String.fromCharCode(part1)+String.fromCharCode(part2);
      return s;
    }
    

    Example:

    > fromOutsideBMP(0x01d122)
      "

提交回复
热议问题