Convert Radix / Number Bases with Node-RED (Decimal, Hexadecimal, Octal, and Binary!)

Sometimes we get some integer value into Node-RED and need to send, store, or process the number in a different format. Maybe a Hexadecmial value will come in and you might need Decimal. Or a binary value comes in but you need Octal – any combination is possible depending on the specific application, so in this post I’ll go over a really simple and and dynamic way to convert numbers between different counting bases or “radix” using JavaScript in a Node-RED function node.

The key here is that you take whatever number is coming in and convert it to a decimal integer using parseInt(), then take that decimal and output it as a converted string, in whatever base you need using toString().
So, in summary, there are three pieces of information for input:

Input value: the number to be converted.
Input radix: the base system of the input.
Output radix: the base system you want to output.

With these three we can easily calculate the new value based on the output radix.


In practice, it might look something like this to convert a hexadecimal (base 16) number to binary (base 2):

image

This simple function node will take some hexadecimal number (base 16) and convert it to binary (base 2) and return that as the new msg.payload in string format. So an input of 4 results in the output of "100" and an input of F would result in "1111".
You can test this yourself with the Node-RED flow text at the end of this post.

To convert from a different input radix, just replace the base number 16 (for hexadecimal) in the parseInt() function with whatever input you are reading in.

To get a different output radix, just replace the number 2 (for binary) in the toString() function with whatever format you want the output to be in.

That’s all there is to it!
There is a sample flow below for you to test this out and experiment with it for yourself – please drop a message in the thread below if you find this useful or have any questions.

And as always, happy coding!


Import text for a sample flow to convert hexadecimal input to binary, octal, and decimal:

[{"id":"743b24f6.14e19c","type":"function","z":"eedf3627.4e1558","name":"convert","func":"input = msg.payload; // Input with Radix 16 = HEX.\n// * Use this input base value for all parseInt() function calls.\n\nBIN = parseInt(input, 16).toString(2); // Radix 2 = BINARY output\nOCT = parseInt(input, 16).toString(8); // Radix 8 = OCTAL output\nDEC = parseInt(input, 16); // parseInt will default to Radix 10 = DECIMAL output\n\nmsg.payload = { \"Binary\" : BIN, \"Octal\" : OCT, \"Decimal\" : DEC };\n\nreturn msg;","outputs":1,"noerr":0,"x":460,"y":2160,"wires":[["60b50f5c.3a867"]]},{"id":"60b50f5c.3a867","type":"debug","z":"eedf3627.4e1558","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":630,"y":2160,"wires":[]},{"id":"2b5a9c2a.9f80d4","type":"inject","z":"eedf3627.4e1558","name":"","topic":"","payload":"4","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2160,"wires":[["743b24f6.14e19c"]]},{"id":"24954458.78125c","type":"inject","z":"eedf3627.4e1558","name":"","topic":"","payload":"B","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2200,"wires":[["743b24f6.14e19c"]]},{"id":"a748ed51.671c9","type":"inject","z":"eedf3627.4e1558","name":"","topic":"","payload":"F","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2240,"wires":[["743b24f6.14e19c"]]},{"id":"8a1c6413.f8fd38","type":"inject","z":"eedf3627.4e1558","name":"","topic":"","payload":"10","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2280,"wires":[["743b24f6.14e19c"]]},{"id":"c82cdfcb.28532","type":"inject","z":"eedf3627.4e1558","name":"","topic":"","payload":"16","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2320,"wires":[["743b24f6.14e19c"]]},{"id":"c8fef115.d3d8d","type":"comment","z":"eedf3627.4e1558","name":"HEX","info":"","x":250,"y":2120,"wires":[]},{"id":"7ef7e304.b0d17c","type":"inject","z":"eedf3627.4e1558","name":"","topic":"","payload":"42","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2360,"wires":[["743b24f6.14e19c"]]},{"id":"bc8ed584.fe3a58","type":"inject","z":"eedf3627.4e1558","name":"","topic":"","payload":"7D","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2400,"wires":[["743b24f6.14e19c"]]},{"id":"2e91aa16.f637e6","type":"inject","z":"eedf3627.4e1558","name":"","topic":"","payload":"3E7","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":2440,"wires":[["743b24f6.14e19c"]]}]
1 Like