How to convert one emoji character to Unicode codepoint number in JavaScript?

前端 未结 5 662
囚心锁ツ
囚心锁ツ 2020-12-03 15:25

how to convert this

5条回答
  •  温柔的废话
    2020-12-03 16:19

    Please Read This Link.

    Here is the function :

    function toUTF16(codePoint) {
    var TEN_BITS = parseInt('1111111111', 2);
    function u(codeUnit) {
      return '\\u'+codeUnit.toString(16).toUpperCase();
    }
    
    if (codePoint <= 0xFFFF) {
      return u(codePoint);
    }
    codePoint -= 0x10000;
    
    // Shift right to get to most significant 10 bits
    var leadSurrogate = 0xD800 + (codePoint >> 10);
    
    // Mask to get least significant 10 bits
    var tailSurrogate = 0xDC00 + (codePoint & TEN_BITS);
    
     return u(leadSurrogate) + u(tailSurrogate);
    }
    

提交回复
热议问题