19

如何在javascript中将其转换为this1f600

''.charCodeAt(0);  

这将返回 unicode 55357 但如何从

4

6 回答 6

22

两种方式

let hex = "".codePointAt(0).toString(16)
let emo = String.fromCodePoint("0x"+hex);

console.log(hex, emo);

于 2020-01-17T18:34:40.733 回答
16

添加了脚本以在浏览器端进行转换

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

于 2018-01-24T10:11:27.730 回答
10

这就是我使用的:

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);
};
于 2019-04-20T15:11:56.887 回答
5

请阅读此链接

这是功能:

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);
}
于 2018-01-24T10:10:15.747 回答
3

这是另一种方式。资源

 "".codePointAt(0).toString(16)
于 2019-12-03T08:56:39.540 回答
0

对于 emoji 到 unicode 的转换,您可以使用emoji-unicode包:

const emojiUnicode = require("emoji-unicode");
   
console.log(emojiUnicode(""));
// => 1f525
于 2020-12-10T19:49:38.617 回答