Number to UTF-8 char

Viewed 56

In Node.js I'm able to print to the console a Japanese character like this:

console.log('\u3041');

If I have 3041 as a number, for instance because it is randomly generated, how do I print the corresponding UTF-8 sigil?

const charNumber = 3041;

// of course this doesn't work, but I need something like that:
console.log(`\u${charNumber}`); 
1 Answers

You can use the HEX representation with .fromCharCode by replacing \u with 0x:

const charNumber = 3041;
console.log(String.fromCharCode(`0x${charNumber}`));

Related