简体   繁体   English

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

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

how to convert this如何转换这个into this 1f600 in javascript进入1f600中的这个1f600

'😀'.charCodeAt(0);  

this will return unicode 55357 but how to get 1f600 from这将返回 unicode 55357 但如何从中获取 1f600

Added script to convert this on browser side添加了在浏览器端进行转换的脚本

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"

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

Two way两种方式

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

This is what I use:这是我使用的:

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

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

Here is another way.这是另一种方式。 Source 来源

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

Emojis like ⚕️ have two parts: +⚕️.像⚕️这样的表情符号有两个部分:+⚕️。
Here is how to get their code:以下是获取代码的方法:

 emoji = "⚕️" // corresponds to 1f469-200d-2695-fe0f code1 = emoji.codePointAt(0).toString(16) // gives only 1f469 code2 = [...emoji].map(e => e.codePointAt(0).toString(16)).join(`-`) // gives correctly 1f469-200d-2695-fe0f console.log(code1) console.log(code2)

For emoji to unicode convertion you can use emoji-unicode package:对于 emoji 到 unicode 的转换,您可以使用emoji-unicode包:

const emojiUnicode = require("emoji-unicode");
   
console.log(emojiUnicode("🔥"));
// => 1f525
const 
  getUnicodeHex = char => char.codePointAt(0).toString(16),    
  getEmoji = unicodeHex => String.fromCodePoint(unicodeHex)

console.log(
  getUnicodeHex('😀'),  // 1f600
  getEmoji(0x1f600)     // 😀
)

Best answer in my view is to use node-emoji package.我认为最好的答案是使用 node-emoji package。

https://www.npmjs.com/package/node-emoji https://www.npmjs.com/package/node-emoji

Here are steps.这是步骤。

  1. do npm i node-emoji做 npm i node-emoji

     var emoji = require('node-emoji'); var convertEmoji = function(data){ if(emoji.hasEmoji(data)){ return emoji.unemojify(data); } else{ return data; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM