I need to get a string / char from a unicode charcode and finally put it into a DOM TextNode to add into an HTML page using client side JavaScript.
Currently, I am doing:
String.fromCharCode(parseInt(charcode, 16));
where charcode
is a hex string containing the charcode, e.g. "1D400"
. The unicode character which should be returned is , but a
퐀
is returned! Characters in the 16 bit range (0000
... FFFF
) are returned as expected.
Any explanation and / or proposals for correction?
Thanks in advance!
The problem is that characters in JavaScript are (mostly) UCS-2 encoded but can represent a character outside the Basic Multilingual Plane in JavaScript as a UTF-16 surrogate pair.
The following function is adapted from Converting punycode with dash character to Unicode: