Reputation: 1510
I want to convert Unicode Code Points Notation like "U+1F1FA U+1F1F8" to "🇺🇸".
In javaScript, what I have tried so far is
String.fromCharCode(parseInt("U+1F1FA", 16));
But this doesn't work.
Upvotes: 4
Views: 722
Reputation: 1510
Thanks a lot for above answers. I came with a method to convert the unicode notations to Emoji.
function unicodeToChar(text:string) {
const splited = text.split(" ")
let str = ""
for(let i = 0;i < splited.length;i++){
splited[i] = splited[i].replace('U','0')
splited[i] = splited[i].replace('+','x')
str += String.fromCodePoint(parseInt(splited[i],16))
}
return str
}
Upvotes: 1
Reputation: 9300
You can use the static method String.fromCodePoint()
to achieve this.
const us = String.fromCodePoint('0x1F1FA', '0x1F1F8');
console.log(us);
Upvotes: 2
Reputation: 433
You can wrap the hex code in braces like this:
console.log('\u{1f1fa}\u{1f1f8}')
Upvotes: 4