如何在JavaScript中将一个表情符号转换为Unicode代码点号?

时间:2018-01-24 09:41:26

标签: javascript emoji emojione

如何将此转换为 javascript中的1f600

''.charCodeAt(0);  

这将返回unicode 55357,但如何从

获得1f600

5 个答案:

答案 0 :(得分:5)

添加了在浏览器端转换此内容的脚本

function emojiUnicode (emoji) {
    var comp;
    if (emoji.length === 1) {
        comp = emoji.charCodeAt(0);
    }
    comp = (
        (emoji.charCodeAt(0) - 0xD800) * 0x400
      + (emoji.charCodeAt(1) - 0xDC00) + 0x10000
    );
    if (comp < 0) {
        comp = emoji.charCodeAt(0);
    }
    return comp.toString("16");
};
emojiUnicode(""); # result "1f600"

感谢https://www.npmjs.com/package/emoji-unicode

答案 1 :(得分:2)

请阅读This Link

这是功能:

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);
}

答案 2 :(得分:1)

这是我使用的:

const toUni = function (str) {
  if (str.length < 4)
    return str.codePointAt(0).toString(16);
  return str.codePointAt(0).toString(16) + '-' + str.codePointAt(2).toString(16);
};

答案 3 :(得分:0)

这是另一种方式。 Source

 "?".codePointAt(0).toString(16)

答案 4 :(得分:0)

要将表情符号转换为unicode,您可以使用emoji-unicode软件包:

const emojiUnicode = require("emoji-unicode");
   
console.log(emojiUnicode("?"));
// => 1f525