如何将一个表情符号字符转换为 JavaScript 中的 Unicode 代码点编号?
How to convert one emoji character to Unicode codepoint number in JavaScript?
如何将 </code> 转换为 <strong>javascript</strong></p> 中的 <code>1f600
''.charCodeAt(0);
这将 return unicode 55357 但如何从
获取 1f600
请阅读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);
}
添加了脚本以在浏览器端转换它
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"
这是我用的:
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);
};
这是另一种方法。 Source
"".codePointAt(0).toString(16)
双向
let hex = "".codePointAt(0).toString(16)
let emo = String.fromCodePoint("0x"+hex);
console.log(hex, emo);
要将表情符号转换为 unicode,您可以使用 emoji-unicode 包:
const emojiUnicode = require("emoji-unicode");
console.log(emojiUnicode(""));
// => 1f525
我认为最好的答案是使用 node-emoji 包。
https://www.npmjs.com/package/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;
}
}
如何将 </code> 转换为 <strong>javascript</strong></p> 中的 <code>1f600
''.charCodeAt(0);
这将 return unicode 55357 但如何从
获取 1f600请阅读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);
}
添加了脚本以在浏览器端转换它
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"
这是我用的:
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);
};
这是另一种方法。 Source
"".codePointAt(0).toString(16)
双向
let hex = "".codePointAt(0).toString(16)
let emo = String.fromCodePoint("0x"+hex);
console.log(hex, emo);
要将表情符号转换为 unicode,您可以使用 emoji-unicode 包:
const emojiUnicode = require("emoji-unicode");
console.log(emojiUnicode(""));
// => 1f525
我认为最好的答案是使用 node-emoji 包。
https://www.npmjs.com/package/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; } }