Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript unescape hex to string

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 😐?

like image 956
Vu Nguyen Avatar asked Oct 31 '22 11:10

Vu Nguyen


1 Answers

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.)

like image 131
Amadan Avatar answered Nov 12 '22 16:11

Amadan