I'm struggling to convert a Counter64 value issued from an SNMP node into an appropriately sized integer. My understanding is that BigInt is now officially part of node.js. So I assumed it would also be available to Node-RED and I've tried the following in a function node:
msg.payload = BigInt(msg.payload, 16); #I've also tried using a base of 256 with the same result
return msg;
This will generate an "SyntaxError: Cannot convert #⏆ to a BigInt"
I then get "TypeError: BigInt.fromArray is not a function"
So it appears that even though BigInt is defined it is not implemented as expected.
So I've tried "npm install big-integer" under the ~/.node-red directory, modifying settings.js and adding bigInt:require("big-integer") under functionGlobalContext and modifying my function node as so:
var bigInt=global.get('bigint');
msg.payload = bigInt.fromArray(msg.payload, 16);
return msg;
Which now gives me this: "TypeError: digits[i].times is not a function"
Or if try this way:
var bigInt=global.get('bigint');
msg.payload = bigInt(msg.payload, 16);
return msg;
I get: "Error: # is not a valid character"
So there's progress I guess but still no success...
I don't think I mentioned I'm running Node-RED on Rpi (Jessie) and I wasn't able to get Node-RED to use the latest version of node.js. So I started from scratch on the latest Raspbian Buster and currently have v12.13.1 and v13.2.0 both installed through nvm and have set 12.13.1 as default as per these instructions but Node-RED always seems to use version 10.15.2 which came preinstalled with Raspbian.
How can I force NodeRED to use a different version of node.js?
Just looking at this for 1st time - it seems that there was an npm package created to give big number manipulation but in recent javascript, it it now native
The syntax for using each method is different
The npm one seems to be called big-integer (and examples use bigInt)
The native one seems to be called BigInt
Try out some of the examples of each to see which one works and then go with that
Also, are you sure msg.payload is something valid that can be converted to a big-integer/BigInt
Turns out the second BigInt you mentioned is the one currently implemented as a built-in object and it doesn't implement a constructor accepting a buffer (a Counter64 object is actually a byte array) and has no functions which would allow conversion from a buffer to a BigInt such as bigInt.fromArray()... It appears to be currently only limited to holding large numbers, performing comparisons and doing math (with some limitations).
So I guess I'll have to rely on an external node instead or come up with my own function to convert a byte array to a 64 bit integer.
You have to tell NR about it as well by adding a setting in the settings.js file.. Have you done this? It is described in the first posting in this thread