Your C code expects a UTF-8 string (character is represented as 4 bytes). The presented JS representation that you see is UTF-16 , however (the symbol is represented as 2 uint16 s, a surrogate pair).
First you need to get the code point (Unicode) for your character (from the JS UTF-16 string), and then create a UTF-8 representation for it.
With ES6, you can use the codePointAt method for the first part, which I would recommend using as a pad, even if it is not supported. I think you do not want to decode surrogate pairs yourself :-)
Otherwise, I don't think there is a library method, but you can write it according to the specification:
function hex(x) { x = x.toString(16); return (x.length > 2 ? "\\u0000" : "\\x00").slice(0,-x.length)+x.toUpperCase(); } var c = "😄"; console.log(c.length, hex(c.charCodeAt(0))+hex(c.charCodeAt(1))); // 2, "\uD83D\uDE04" var cp = c.codePointAt(0); var bytes = new Uint8Array(4); bytes[3] = 0x80 | cp & 0x3F; bytes[2] = 0x80 | (cp >>>= 6) & 0x3F; bytes[1] = 0x80 | (cp >>>= 6) & 0x3F; bytes[0] = 0xF0 | (cp >>>= 6) & 0x3F; console.log(Array.prototype.map.call(bytes, hex).join("")) // "\xf0\x9f\x98\x84"
(tested in Chrome)
source share