javascript unescape hex to string

时间:2016-01-19 04:47:08

标签: javascript escaping

我有十六进制代码1f610,因此格式字符串为\u{1f610}且显示。但是我如何从十六进制代码中解除它呢?

我做了

var code = '1f610';

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

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

我该怎么办才能将其转移到

1 个答案:

答案 0 :(得分:2)

这是一个星体设置字符,需要JavaScript字符串中的两个字符。

改编自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>

(另请注意,unescape函数已弃用。)