Javascript unescape hex for string

I have a 1f610 hex code, so the format string \u{1f610} with 😐 is displayed. But how can I cancel it from hex code?

I did

 var code = '1f610'; unescape('%u' + code); //=> ὑ0 unescape('%u' + '{' + code + '}'); //=> %u{1f610} 

What to do to cancel it before 😐 ?

+5
source share
1 answer

This is an astral character set that 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.imtqy.com/stack-snippet-console/console.min.js"></script> 

(Also note that the unescape function is deprecatd.)

+2
source

Source: https://habr.com/ru/post/1240892/


All Articles