如何在 JavaScript 中将此 转换为
1f600
?
''.charCodeAt(0);
这将返回unicode 55357,但如何从中获取1f600?
如何在 JavaScript 中将此 转换为
1f600
?
''.charCodeAt(0);
这将返回unicode 55357,但如何从中获取1f600?
双向
let hex = "".codePointAt(0).toString(16)
let emo = String.fromCodePoint("0x"+hex);
console.log(hex, emo);
console.log( String.fromCodePoint("0xD83D","0xDE0A") )
,我得到的是 - 但如果我改变这两个字符串的顺序,我会得到��,如果我将它们连接成"0xD83DDE0A",我会收到异常。 - Kamil Kiełczewski已添加脚本以在浏览器端进行转换
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);
};
请阅读 此链接。
以下是函数:
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);
}
这里有另外一种方法。来源
"".codePointAt(0).toString(16)
像 ⚕️ 这样的表情符号由两部分组成:+⚕️。
以下是获取它们的代码的方法:
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)
emoji_code = "-".join(f"{ord(c):x}" for c in "⚕️")
在Python中的相应实现。 - Kolibrilconst
getUnicodeHex = char => char.codePointAt(0).toString(16),
getEmoji = unicodeHex => String.fromCodePoint(unicodeHex)
console.log(
getUnicodeHex(''), // 1f600
getEmoji(0x1f600) //
)
在我看来,最好的答案是使用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;
}
}