JavaScript将emoji字符转换为Unicode
String.prototype.codePointAt
The codePointAt
method returns a non-negative integer that is the Unicode code point value at the given position.
'☃★♲'.length
// output: "3"
'☃★♲'.codePointAt(0);
// output: "9731"
'☃★♲'.codePointAt(1);
// output: "9733"
'☃★♲'.codePointAt(2);
// output: "9842"
'😀'.length
// output: "2"
"😀".codePointAt(0);
// output: "128512"
"😀".codePointAt(1);
// output: "56832"
"😀".codePointAt(0).toString(16);
// output: "1f600"
'🇺🇸'.length
// output: "4"
"🇺🇸".codePointAt(0);
// output: "127482"
"🇺🇸".codePointAt(1);
// output: "56826"
"🇺🇸".codePointAt(2);
// output: "127480"
"🇺🇸".codePointAt(3);
// output: "56824"
String.fromCodePoint
The static String.fromCodePoint
method returns a string created by using the specified sequence of code points.
String.fromCodePoint(9731, 9733, 9842, 0x2F804);
// output: "☃★♲你"
String.fromCodePoint(128512, 56832);
// output: "😀�"
String.fromCodePoint(128512);
// output: "😀"
String.fromCodePoint("0x"+"1f600");
// output: "😀"
String.fromCodePoint(127482,127480);
// output: "🇺🇸"
转换多个 emoji
[...'☃★♲你😀🇺🇸'].map((el)=>{return el.codePointAt(0);})
// output: [9731, 9733, 9842, 20320, 128512, 127482, 127480]
String.fromCodePoint(...[9731, 9733, 9842, 20320, 128512, 127482, 127480])
// output: "'☃★♲你😀🇺🇸'"
[...'☃★♲你😀🇺🇸'].map((el)=>{return `0x${el.codePointAt(0).toString(16)}`;})
// output: ['0x2603', '0x2605', '0x2672', '0x4f60', '0x1f600', '0x1f1fa', '0x1f1f8']
String.fromCodePoint(...['0x2603', '0x2605', '0x2672', '0x4f60', '0x1f600', '0x1f1fa', '0x1f1f8'])
// output: '☃★♲你😀🇺🇸'
for (let ch of '☃★♲你😀🇺🇸') {
console.log(`0x${ch.codePointAt(0).toString(16)}`);
}
// 0x2603
// 0x2605
// 0x2672
// 0x4f60
// 0x1f600
// 0x1f1fa
// 0x1f1f8
Unicode encode/Unicode decode/Emoji encode/Emoji decode - online