UTF-16 Unicode Characters with Node-RED JavaScript

If you need your Node-RED flow to work with complex characters like foreign languages for internationalization (or emojis) it can be a bit tricky to know where to start. In this post I’ll break down a few basic examples to explain how to use it and how it works.

The critical function I’ll be focusing on is String.fromCharCode(), it consumes one or more integer or hex numbers and returns the associated character. For example the Unicode character U+1F408 is represented in UTF-16 by hex 0xD83D 0xDC08, which String.fromCharCode() will convert to “:cat2:”.

Similarly, String.fromCharCode(0xC548) will return the Korean character 안. For multiple characters, you can simply hand in a comma-separated list of symbols to get longer sentences, such as String.fromCharCode(0xC548, 0xB155, 0x0020, 0xC138, 0xC0C1) which gives the result “안녕 세상”.
image

If you have your data in the form 0xC548B155 or 0xC5 0x48 0xB1 0x55 you need to do some processing so that you don’t get an error, or four separate characters “ÅH±U” instead of the expected “안녕”.
This is easily solved with a for loop to create 4-character strings and prepend them with 0x, just be aware of the format of your data before putting it into the string function or you will have a lot of trouble debugging your code.
Here’s an example:

var input = "C548B155C138C0C1";
var output = "";
for (var i = 0; i < input.length; i+=4) {
    var nextChar = '0x' + input.substring(i, i + 4);
    output += String.fromCharCode(nextChar);
}
msg.payload = output;
return msg;

// → msg.payload : string[4] = "안녕세상"

If you end up using this in your application, or have any questions about it, please post in the thread below. And as always, happy coding!

4 Likes