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

如何在JavaScript中将一个表情符号转换为Unicode代码点编号?,javascript,emoji,emojione,Javascript,Emoji,Emojione,如何转换此请阅读 以下是函数: 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; // Shi

如何转换此
请阅读

以下是函数:

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);
}
函数toUTF16(代码点){
变量十位=parseInt('1111111',2);
功能u(代码单元){
返回'\\u'+codeUnit.toString(16.toUpperCase();
}
如果(码点>10);
//掩码以获得最低有效10位
var TAILSURRONATE=0xDC00+(码点和十位);
返回u(引导代理)+u(引导代理);
}

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

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("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);
};
函数emojionicode(表情符号){
var-comp;
if(emoji.length==1){
comp=emoji.charCodeAt(0);
}
薪酬=(
(emoji.charCodeAt(0)-0xD800)*0x400
+(emoji.charCodeAt(1)-0xDC00)+0x10000
);
if(comp<0){
comp=emoji.charCodeAt(0);
}
返回公司toString(“16”);
};
emojiUnicode(这是我使用的:

const toUni=函数(str){
如果(str.length<4)
返回str.codePointAt(0).toString(16);
返回str.codePointAt(0).toString(16)+'-'+str.codePointAt(2).toString(16);
};
这里是另一种方法

“双向


let hex=“对于表情符号到unicode的转换,您可以使用软件包:

const emojionicode=require(“emoji-unicode”);

控制台日志(emojiUnicode(“此npm软件包具有转换为Unicode的方法。@gurvinder372我已经尝试过您的解决方案,但它不起作用。这不是重复的question@MitulGedeeya如何在浏览器中执行???@ParthGajjar共享您在问题本身中尝试过的内容,以便我们知道您已经尝试过的内容。创建一个工作片段来演示问题本身。Sorry它将是服务器端渲染。:)让我用错误的函数来回答你。当你从原始函数中删除
返回
,并且没有添加正确的
ifs
elses
时,你改变了函数。正确的做法是:如果emojis是另外两个函数的组合,并且charCodeAt从0到4,那么它会是怎样的呢?例如:(Javascript),如果您需要再次将其转换回表情符号,请使用:String.fromCodePoint(parseInt(“1f600”,16))国旗不显示properly@ParthGajjar:先生,你能告诉我如何从\uD83D\uDE0A这个字符串中获取表情符号吗?在我看来,这个Proso(解决方案的提供者)有正确的想法。根据Mozilla codePointAt专门用于处理表情或图标。我的错误是,toString(16)是获取十六进制值。非常感谢在这里使用反向函数!\uD83D\uDE0A先生,我需要将提到的uniquecode转换为表情?@Kapilsoni
console.log(String.fromCodePoint(“0xD83D”,“0xDE0A”)
(我明白了:@KamilKiełczewski:sir我的case unicode字符串是\uD83D\uDE0A。如果直接从代码点放入\uD83D\uDE0A,它就不起作用了。我可以转换成其他格式吗?如果你想让它在RESP API中使用json工作,最好的解决方案。它会给出像“\uD83D\uDE00”这样的输出。)(要获得好的答案,请将其与此数据源结合起来:这样您就有了一种将表情转换为快捷方式的好方法