Node-red component for handling big integers

#1

Is there any node-red component available, for handling big integers (Int64, UInt64) ? I need to combine into a function node two 32-bit integers into a 64-bit integer.
Thx for your help.

0 Likes

#2

I don't know of any pre-built nodes for handling Big Integers -- but there are many NPM modules that can do that. The one I've used is called integer... you can either require it in your settings.js file, or use the node-red-contrib-npm node to load it at run time.

Here is one way you could set the node properties to call the necessary function to build an int-64 from two int-32 numbers:

image

The down-side of this node is that the target npm module is reloaded from the npm repo every time that node is restarted. So it would be more efficient to add this module into your global function context, and then use a function node to call the fromBits() method directly. I'll leave that as an exercise for the reader ;*)

In the meantime, here is a small flow that shows the module working.

[{"id":"9287e604.ca8408","type":"npm","z":"dbef1f69.ea3a3","name":"big integer","func":"// get the input 32-bit integers, as necessary\nvar lo = msg.payload.lo;\nvar hi = msg.payload.hi || 0;\n\n// NPM module exposed as variable, npm_module\nvar i64 = npm_module.fromBits(lo, hi);\n\n// retrun the 64-bit integer as a string\nreturn i64.toString();","npm_module":"integer","module_style":"custom","msg_payload":"return_val","function_name":"frombits","x":410,"y":340,"wires":[["a183b34.d78875"]]},{"id":"3ab5db05.c03434","type":"inject","z":"dbef1f69.ea3a3","name":"","topic":"","payload":"{\"hi\":32,\"lo\":64}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":340,"wires":[["9287e604.ca8408"]]},{"id":"a183b34.d78875","type":"debug","z":"dbef1f69.ea3a3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":610,"y":340,"wires":[]}]

BTW, the ability to execute math functions on Big Integers would make a nice addition to the contributed nodes in the flows library... if you are interested in writing custom nodes.

0 Likes

#3

Thx for your reply. Got it working with an extra npm module that I added in the global function context (in settings.js).

Looks like "BigInt" will very soon be a native JavaScript feature and will then probably be also available directly in the NodeRed function nodes.

https://github.com/tc39/proposal-bigint/blob/master/README.md
https://tc39.github.io/proposal-bigint/

1 Like