I have a hex code 1f610
, so the format string is \u{1f610}
with 😐
in display. But how can I unescape it from the hex code?
I did
var code = '1f610';
unescape('%u' + code); //=> ὡ0
unescape('%u' + '{' + code + '}'); //=> %u{1f610}
what should I do to unescape it to 😐
?
This is an astral set character, which requires two characters in a JavaScript string.
Adapted from Wikipedia:
var code = '1f610';
var unicode = parseInt(code, 16);
var the20bits = unicode - 0x10000;
var highSurrogate = (the20bits >> 10) + 0xD800;
var lowSurrogate = (the20bits & 1023) + 0xDC00;
var character = String.fromCharCode(highSurrogate) + String.fromCharCode(lowSurrogate);
console.log(character);
<!-- results pane console output; see http://meta.stackexchange.com/a/242491 -->
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
(Also note that unescape
function is deprecatd.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With