2016-01-19 6 views
5

Ich habe einen Hex-Code 1f610, so dass die Formatzeichenfolge \u{1f610} mit im Display ist. Aber wie kann ich es aus dem Hex-Code entschlüsseln?Javascript Unescape Hex zu String

Ich habe

var code = '1f610'; 

unescape('%u' + code); //=> ὡ0 

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

, was soll ich tun, um es zu unescape?

+0

Google mich auf diese Antwort darauf: http://stackoverflow.com/questions/4209104/decoding-hex-string-in-javascript – dovidweisz

+0

Ich habe versucht, aber nicht arbeiten, danke @wapsee :) –

Antwort

2

Dies ist ein astrales Zeichen, das zwei Zeichen in einer JavaScript-Zeichenfolge benötigt.

Angepasst von 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>

(Beachten Sie auch, dass unescape Funktion deprecatd ist.)