# Converting buffer array to BigInt integer

**URL:** <https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283>\
**Category:** FAQs\
**Created:** [22 November 2019 13:50 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283 "2019-11-22T13:50:32Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![tohox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tohox/32/14536_2.png) [@tohox](https://discourse.nodered.org/u/tohox)\
**Post date:** [22 November 2019 13:50 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/1 "2019-11-22T13:50:32Z")

</div>

Hi,

I'm struggling to convert a Counter64 value issued from an [SNMP node](https://flows.nodered.org/node/node-red-node-snmp) into an appropriately sized integer. My understanding is that [BigInt](https://www.npmjs.com/package/big-integer) 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:

```auto
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:

```auto
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:

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [22 November 2019 22:44 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/2 "2019-11-22T22:44:37Z")

</div>

Its **bigInt** instead of **BigInt**.

---

<div class="post-metadata">

**Author:** ![tohox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tohox/32/14536_2.png) [@tohox](https://discourse.nodered.org/u/tohox)\
**Post date:** [23 November 2019 04:07 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/3 "2019-11-23T04:07:13Z")

</div>

Hi,

Thanks for you reply.

When using **bigInt** instead of **BigInt** such as in:

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

```

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 November 2019 13:30 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/4 "2019-11-23T13:30:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tohox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tohox/32/14536_2.png) [@tohox](https://discourse.nodered.org/u/tohox)\
**Post date:** [23 November 2019 14:17 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/5 "2019-11-23T14:17:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 November 2019 14:56 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/6 "2019-11-23T14:56:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tohox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tohox/32/14536_2.png) [@tohox](https://discourse.nodered.org/u/tohox)\
**Post date:** [23 November 2019 15:39 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/7 "2019-11-23T15:39:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![tohox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tohox/32/14536_2.png) [@tohox](https://discourse.nodered.org/u/tohox)\
**Post date:** [23 November 2019 21:02 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/8 "2019-11-23T21:02:38Z")

</div>

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](https://nodered.org/docs/faq/node-versions) 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,

---

<div class="post-metadata">

**Author:** ![tohox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tohox/32/14536_2.png) [@tohox](https://discourse.nodered.org/u/tohox)\
**Post date:** [24 November 2019 01:14 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/9 "2019-11-24T01:14:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [24 November 2019 08:57 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/10 "2019-11-24T08:57:08Z")

</div>

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)

> **[big-integer](https://www.npmjs.com/package/big-integer)**
>
> An arbitrary length integer library for Javascript

The native one seems to be called BigInt

> **[BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)**
>
> BigInt is a built-in object that provides a way to represent whole numbers larger than 253 - 1, which is the largest number JavaScript can reliably represent with the Number primitive. BigInt can be used for arbitrarily large integers.

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

---

<div class="post-metadata">

**Author:** ![tohox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tohox/32/14536_2.png) [@tohox](https://discourse.nodered.org/u/tohox)\
**Post date:** [24 November 2019 20:23 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/11 "2019-11-24T20:23:43Z")

</div>

Hi @cymplecy !

Turns out the second [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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!

---

<div class="post-metadata">

**Author:** ![tohox](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tohox/32/14536_2.png) [@tohox](https://discourse.nodered.org/u/tohox)\
**Post date:** [25 November 2019 14:45 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/12 "2019-11-25T14:45:11Z")

</div>

Finally got it working using the [big-integer](https://www.npmjs.com/package/big-integer) external library this way:

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

---

<div class="post-metadata">

**Author:** ![leoneltoledo](https://avatars.discourse-cdn.com/v4/letter/l/e68b1a/32.png) [@leoneltoledo](https://discourse.nodered.org/u/leoneltoledo)\
**Post date:** [31 July 2020 04:17 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/13 "2020-07-31T04:17:01Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [31 July 2020 05:12 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/14 "2020-07-31T05:12:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leoneltoledo](https://avatars.discourse-cdn.com/v4/letter/l/e68b1a/32.png) [@leoneltoledo](https://discourse.nodered.org/u/leoneltoledo)\
**Post date:** [3 August 2020 14:13 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/17 "2020-08-03T14:13:52Z")

</div>

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

```auto
cd .node-red
npm install big-integer

```

- Edit "settings.js" in ./node-red folder, and adding

```auto
        bigInt:require("big-integer") 

```

under functionGlobalContext

- Write inside Node-RED function:

```auto
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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 August 2020 14:14 UTC](https://discourse.nodered.org/t/converting-buffer-array-to-bigint-integer/18283/18 "2020-08-17T14:14:00Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
