Converting buffer array to BigInt integer

Hi,

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"

If I do this:

msg.payload = BigInt.fromArray(msg.payload, 16);
return msg;

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...

Any help will be much appreciated!

Its bigInt instead of BigInt.

Hi,

Thanks for you reply.

When using bigInt instead of BigInt such as in:

msg.payload = bigInt.fromArray(msg.payload, 16);
return msg;

I get: "ReferenceError: bigInt is not defined (line 1, col 15)"

What version of node.js are you using? I think that bigint is only supported from v12? (I might be wrong tho)

I updated to 1.0.2 a few days ago but I see that 1.0.3 is now available.

I'll give it a try...

The question was what version of node.js, not what version of node-red. Run, in a terminal
node -v
to find out, or look in the node-red startup log.

Apologies for the hurried response. Indeed my node.js install appears to be outdated at 10.17 if 12 is the current latest version.

I assumed (wrongly) that updating Node RED would also update node.js...

I'll update and report back.

Thanks!

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?

Thanks,

Ok, finally got Node-RED to use node v12.13.1 but I still get the same error messages when attempting to use BigInt.

  • BigInt exists but when used returns "SyntaxError: Cannot convert &�?( to a BigInt"
  • BigInt.fromArray() returns : "TypeError: BigInt.fromArray is not a function"
  • bigInt isn't recognized and doesn't appear to exist.

So pretty much the same as with v10...

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

Hi @cymplecy !

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.

Thanks to all for your input!

Finally got it working using the big-integer external library this way:

var bigInt = global.get('bigInt');
msg.payload = bigInt.fromArray([...msg.payload], 256);
return msg;

So you can't directly pass a buffer to fromArray() you have to convert it to an array first and base has to be 256 and not 16.

I'm trying to do exactly the same, but I receive the message ""TypeError: Cannot read property 'fromArray' of undefined"

How must be loaded the "big-integer" library? I installed witn "npm install big-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

(post withdrawn by author, will be automatically deleted in 12 hours unless flagged)

1 Like

I didn't before. After that, it's working as expected

So for newbies and semi clueless people (like me), the complete process is:

  • Get into ./node-red folder and Install big-integer
cd .node-red
npm install big-integer
  • Edit "settings.js" in ./node-red folder, and adding
        bigInt:require("big-integer") 

under functionGlobalContext

  • Write inside Node-RED function:
var bigInt = global.get('bigInt');
msg.payload = bigInt.fromArray([...msg.payload], 256);
return msg;

Where "msg.payload" can be replaced for any variable that is stored as buffer

Thank you very much for this information