如何在 JavaScript 中将一个表情符号字符转换为 Unicode 代码点数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48419167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:01:09 来源:igfitidea点击:
How to convert one emoji character to Unicode codepoint number in JavaScript?
提问by Parth Gajjar
how to convert this in to this 1f600in javascript
如何在javascript中将其转换为这个1f600
''.charCodeAt(0);
this will return unicode 55357 but how to get 1f600 from
这将返回 unicode 55357 但如何从中获取 1f600
回答by Parth Gajjar
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"
回答by Vad
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);
};
回答by Kamil Kie?czewski
Two way
两种方式
let hex = "".codePointAt(0).toString(16)
let emo = String.fromCodePoint("0x"+hex);
console.log(hex, emo);
回答by Mitul Gedeeya
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);
}

