# Convert array of UINT (16bit) numbers to array of Double (64bit) numbers

**URL:** <https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258>\
**Category:** General\
**Created:** [12 August 2019 18:50 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258 "2019-08-12T18:50:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![eduardotodero](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@eduardotodero](https://discourse.nodered.org/u/eduardotodero)\
**Post date:** [12 August 2019 18:50 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/1 "2019-08-12T18:50:05Z")

</div>

Guys, I need to join 4 positions of this array and only join 1, turning 4x16bits into 64 bits. So basically I need to merge 4 by 4 sequences into just 1 and turn that 4 16-bit data into a 64-bit one. Can you help me?

 ![regs](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9ed3d7e7fc774bb7d31a26f5ff77c6a18395409b.png)

---

<div class="post-metadata">

**Author:** ![thatcadguy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thatcadguy/32/5273_2.png) [@thatcadguy](https://discourse.nodered.org/u/thatcadguy)\
**Post date:** [12 August 2019 19:03 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/2 "2019-08-12T19:03:56Z")

</div>

Is this still related to the question in the other thread about getting a 64-bit float from 16-bit ints? Did you try what I suggested with the TypedArrays?

---

<div class="post-metadata">

**Author:** ![thatcadguy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thatcadguy/32/5273_2.png) [@thatcadguy](https://discourse.nodered.org/u/thatcadguy)\
**Post date:** [12 August 2019 19:05 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/3 "2019-08-12T19:05:43Z")

</div>

[https://discourse.nodered.org/t/modbus-tcp-read-32-bit-registers/1710/20?u=thatcadguy](https://discourse.nodered.org/t/modbus-tcp-read-32-bit-registers/1710/20)

---

<div class="post-metadata">

**Author:** ![eduardotodero](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@eduardotodero](https://discourse.nodered.org/u/eduardotodero)\
**Post date:** [12 August 2019 19:39 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/4 "2019-08-12T19:39:59Z")

</div>

I was able to transform it to 64 bits (with readDoubleBE), but reading one register at a time. Since I have 160 registers and 40 final information, I wanted to make a function that would gather 4 by 4 ModBus read positions and transform the value optimally by arranging it into just one vector to send to the broker. I tried to make an algorithm but it is not working. Could you help me?

 ![agortmo](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/367e86197ca3af13fcc0b5659301d366ee9b8e62.png)

---

<div class="post-metadata">

**Author:** ![thatcadguy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thatcadguy/32/5273_2.png) [@thatcadguy](https://discourse.nodered.org/u/thatcadguy)\
**Post date:** [12 August 2019 19:51 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/5 "2019-08-12T19:51:07Z")

</div>

Looking at your example, try doing "i += 4" instead of "i+4". Also, not sure that you need the ".value" after msg.payload.

---

<div class="post-metadata">

**Author:** ![eduardotodero](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@eduardotodero](https://discourse.nodered.org/u/eduardotodero)\
**Post date:** [12 August 2019 20:10 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/6 "2019-08-12T20:10:21Z")

</div>

The message that appears in debud is this: ![fdsfdsf](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/25fa7dac27b147edecafd66f3e2e516867de258d.png)  
Looks like it's not incrementing the vector ...

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 August 2019 07:26 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/7 "2019-08-13T07:26:54Z")

</div>

Post some actual data (use the copy button that appears on the right of your debug message when you hover over the data) and the actual function code (not a screen shot but text) placed between two lines of ````` and I will fix it for you.

E.g.  
`````  
`var blah = "blahdiblah";`  
`````

This has gone on too long 😉

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 August 2019 08:36 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/8 "2019-08-13T08:36:45Z")

</div>

Never mind. I wrote it out by hand. In future, please provide sample data and something we can copy paste (screen shots are great for looking at only)

### Sample data I used...

```auto
[16705,46555,13517,48779,0,0,0,0,16466,25789,26863,40598,0,0,0,0,16589,8742,32208,27469]

```

### Function code I wrote...

```auto
var arrDBE = [];//array to store results
var buf = new Buffer(msg.payload.length*2);//create a buffer with enough space

//first loop through and populate buffer with UINT 16 values
//probably a better way but off the top of my head, this works
for(let i=0; i < msg.payload.length; i++){
    let v = msg.payload[i];
    let offset = i*2;
    buf.writeUInt16BE(v,offset);
}

//now loop the buffer and read Double values
//push them into the array
for(let i=0; i < buf.length; i+=8){
    let vBE = buf.readDoubleBE(i);
    arrDBE.push(vBE);
}

//assign the array to msg.doubleData. Doing this
//leaves the original msg.payload in tact
//(you may need it later or wish to debug both sets of data)
msg.doubleData = arrDBE;
return msg;

```

### the flow I created for testing...

```auto
[{"id":"adffb031.38861","type":"inject","z":"86c99817.23b7c8","name":"simulate modbus data","topic":"","payload":"[16705,46555,13517,48779,0,0,0,0,16466,25789,26863,40598,0,0,0,0,16589,8742,32208,27469]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":140,"wires":[["b0b61d1c.7c502"]]},{"id":"b0b61d1c.7c502","type":"function","z":"86c99817.23b7c8","name":"writeUInt16BE, readDoubleBE","func":"var arrDBE = [];//array to store results\nvar buf = new Buffer(msg.payload.length*2);//create a buffer with enough space\n\n//first loop through and populate buffer with UINT 16 values\n//probably a better way but off the top of my head, this works\nfor(let i=0; i < msg.payload.length; i++){\n let v = msg.payload[i];\n let offset = i*2;\n buf.writeUInt16BE(v,offset);\n}\n\n//now loop the buffer and read Double values\n//pusg them into the array\nfor(let i=0; i < buf.length; i+=8){\n let vBE = buf.readDoubleBE(i);\n arrDBE.push(vBE);\n}\n\n//assign the array to msg.doubleData. Doing this\n//leaves the original msg.payload in tact\n//(you may need it later or wish to debug both sets of data)\nmsg.doubleData = arrDBE;\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":180,"wires":[["7b2f88cd.512748"]]},{"id":"7b2f88cd.512748","type":"debug","z":"86c99817.23b7c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":450,"y":220,"wires":[]}]

```

### Screen shot of the flow

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/90274113d2c3d3a77dfe65056b4cf9a2c9b3e6fd.png)

### EDIT TITLE...

I updated the title of this topic to something a bit more descriptive - for future users searching for similar answers.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 August 2019 07:48 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/9 "2019-08-14T07:48:06Z")

</div>

@eduardotodero

Did this work for you?

Perhaps you could mark it as the solution to let future users searching similar requirements know it worked?

---

<div class="post-metadata">

**Author:** ![ilmisu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ilmisu/32/27748_2.png) [@ilmisu](https://discourse.nodered.org/u/ilmisu)\
**Post date:** [26 August 2020 09:56 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/10 "2020-08-26T09:56:52Z")

</div>

Hi guys,  
By starting from this topic, i need to pick up 4 16bit registers from an instrument connected via Modbus and merge all into an UINT64 integer.  
Could you please help me to fine a way?

thanks a lot.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [26 August 2020 10:16 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/11 "2020-08-26T10:16:38Z")

</div>

Have a look at this excellent contrib node: [https://flows.nodered.org/node/node-red-contrib-buffer-parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser)

Handling 64bit values with Javascript can be a bit tricky, though. I think you need to use the new [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) type that should be available in recent NodeJS (\>=10) versions. The contrib node already supports it with the biguint64(be|le) types, depending on your data.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 August 2020 13:13 UTC](https://discourse.nodered.org/t/convert-array-of-uint-16bit-numbers-to-array-of-double-64bit-numbers/14258/12 "2020-08-26T13:13:12Z")

</div>

@ilmisu See answer I posted to a very similar question [here](https://discourse.nodered.org/t/read-uint64-from-a-se-iem3250/32038/2)
