# How to read a 33-bit field with "node-red-contrib-binary"?

**URL:** https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483
**Category:** General
**Created:** [30 January 2022 07:07 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483 "2022-01-30T07:07:05Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)
#### Post date: [30 January 2022 07:07 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/1 "2022-01-30T07:07:05Z")

</div>

Hi there,

I'm currently using the node-red-contrib-binary, but I'm facing an issue whereby I can't seem to be able to read a 33-bit field.

No issue at all for 1-32 bit, but for 33, I have no clue... Has anybody been able to do so ?

---

<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: [30 January 2022 08:38 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/2 "2022-01-30T08:38:23Z")

</div>

Easy enough with buffer parser node.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5ae0de004748ae49939d07ab9ae42bdcfd901d24.png)

```auto
[{"id":"af1d307c4cb20277","type":"buffer-parser","z":"85e1791bc0cd6285","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bigint64be","name":"MASK_00000001FFFFFFFF","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":"0x00000001FFFFFFFF"},{"type":"bigint64be","name":"MASK_000001FFFFFFFFFF","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":"0x000001FFFFFFFFFF"},{"type":"bigint64be","name":"MASK_0001FFFFFFFFFFFF","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":"0x0001FFFFFFFFFFFF"},{"type":"bigint64be","name":"MASK_000FFFFFFFFFFFFF","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":"0x000FFFFFFFFFFFFF"},{"type":"bigint64be","name":"NO_MASK","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2170,"y":140,"wires":[["8c178d9160e32087"]]},{"id":"9f6b3d376054a695","type":"inject","z":"85e1791bc0cd6285","name":"0x000FFFFFFFFFFFFF (4503599627370495)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[0,15,255,255,255,255,255,255, 0,0,0,128,0,0,51 , 0,0,0,0,129,5,5,1]","payloadType":"bin","x":2070,"y":80,"wires":[["af1d307c4cb20277"]]},{"id":"8c178d9160e32087","type":"debug","z":"85e1791bc0cd6285","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2170,"y":200,"wires":[]}]

```

NOTE: This makes use of `BigInt` type. Your node-js version must be V10.4.0 or greater.  
NOTE2: As the result is a BigInt, you might need to change it to a `number` type (easy in a function node with `msg.payload.NO_MASK = Number(msg.payload.NO_MASK);`)

---

<div class="post-metadata">

### Author: ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)
#### Post date: [30 January 2022 09:07 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/3 "2022-01-30T09:07:31Z")

</div>

Thanks a lot @Steve-Mcl !

While I understand how to navigate through the whole buffer with "node-red-contrib-binary" (My buffer total length is 320 bits), I am not sure to know how to use buffer-parser to do the same.

Would you be able to briefly give me an example over the first say, 16 bits ?

---

<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: [30 January 2022 09:20 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/4 "2022-01-30T09:20:43Z")

</div>

> [@janedoe123](#):
>
> (My buffer total length is 320 bits), I am not sure to know how to use buffer-parser to do the same.

Simply add entrys that relate to the byte offset in the incoming data.  
Read the [readme](https://flows.nodered.org/node/node-red-contrib-buffer-parser) and built in help particularly this bit...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1a90c639866f41156d514830efd1f3b375b4d178.png)

> [@janedoe123](#):
>
> Would you be able to briefly give me an example

Sure, but firstly, have you looked at the built in demos?  
CTRL+I → examples → node-red-contrib-buffer-parser  
Give them a go to see if it clears things up

---

<div class="post-metadata">

### Author: ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)
#### Post date: [30 January 2022 10:24 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/5 "2022-01-30T10:24:29Z")

</div>

Thank you very much, @Steve-Mcl !

I'm able to progress but now I'm stuck with the Mask usage:

- The 33-bit value I need to read starts at offset 175 (bits)
- So I need to start reading the bigInt64 at offset 168 (bits), in another word offset 21 (bytes).

But how to extract the right 33 bit ?

---

<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: [30 January 2022 10:26 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/6 "2022-01-30T10:26:42Z")

</div>

Can you provide sample data (use the copy value button in debug sidebar) and what bytes contain what values (and the value you expect it to be)

PS, are you mixing up `bits` and `bytes` ?

---

<div class="post-metadata">

### Author: ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)
#### Post date: [30 January 2022 10:30 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/7 "2022-01-30T10:30:27Z")

</div>

Hi @Steve-Mcl here it is. The 33-bit long value I need to extract is positioned at the 175th bit (offset), and the integer value is as follows:

Sample data 1:

[252,48,32,0,0,0,0,0,0,0,255,240,15,5,0,15,168,66,127,79,254,50,172,56,15,0,0,0,0,0,0,148,70,103,218]

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/113ccac9793c2902711c1ff992f28698159b530b.png)

Sample data 2:

[252,48,37,0,0,0,0,0,0,0,255,240,20,5,0,15,168,64,127,239,254,50,196,213,188,254,0,247,49,64,0,0,0,0,0,0,6,231,201,95]

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f9c36b70691be2c9df2dd2f1e768c67162bfea6.png)

PS: Not mixing up bits and bytes, just that Node-RED Buffer parser offset is in bytes, but on the other app I'm using, the offset is expressed in bits 🙂

---

<div class="post-metadata">

### Author: ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)
#### Post date: [30 January 2022 11:19 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/8 "2022-01-30T11:19:43Z")

</div>

By the way @Steve-Mcl

Every time I'm trying to use Mask, I keep getting the following error:

> "TypeError: Cannot mix BigInt and other types, use explicit conversions"

I really got no idea how to use the mask, even after reading the readme and all... Would you be able to help?

---

<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: [30 January 2022 11:24 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/9 "2022-01-30T11:24:06Z")

</div>

> [@janedoe123](#):
>
> "TypeError: Cannot mix BigInt and other types, use explicit conversions"

You possibly have an older version (it was a bug)

check you have V3.2.1 installed.

---

<div class="post-metadata">

### Author: ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)
#### Post date: [30 January 2022 11:26 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/10 "2022-01-30T11:26:28Z")

</div>

Hi @Steve-Mcl

Which version ?

I installed the latest nodeJS (17.4.0) today morning as well as NodeRED 2.2.0, same problem.

---

<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: [30 January 2022 11:29 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/11 "2022-01-30T11:29:39Z")

</div>

> [@Steve-Mcl](#):
>
> check you have V3.2.1 installed.

[node-red-contrib-buffer-parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser) v3.2.1

> [@janedoe123](#):
>
> I installed the latest nodeJS (17.4.0) today

I would suggest sticking with node v16 LTS

---

<div class="post-metadata">

### Author: ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)
#### Post date: [30 January 2022 11:32 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/12 "2022-01-30T11:32:34Z")

</div>

@Steve-Mcl

I was running v3.2.0, now running 3.2.1. Will check if I still get the error or not.

In the meanwhile, would you mind helping me understand how the mask would work in my case, and how to apply it?

---

<div class="post-metadata">

### Author: ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)
#### Post date: [30 January 2022 11:35 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/13 "2022-01-30T11:35:24Z")

</div>

I'm still facing the issue as shown in the screenshot.

Am I using Mask wrongly ?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/5/f5ecff41f1e5b6a15759fe6d496fc7d6bc1fbb59.png)

---

<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: [30 January 2022 11:41 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/14 "2022-01-30T11:41:54Z")

</div>

Yes, you found a bug 🙂 (its the UInt64 type mask isnt parsed correctly)

i have fixed it and released V3.2.2 - it will take 10m or so for the flows lib to become avaiable on your node-red palette manager.

Refresh node-red and see if 3.2.2 is available to update (or use NPM on the terminal if you know how)

---

<div class="post-metadata">

### Author: ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)
#### Post date: [30 January 2022 11:43 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/15 "2022-01-30T11:43:02Z")

</div>

Alright, thanks a lot !

Sorry for asking again about the Mask, how can I use it to extract only the 33-bit that I am interested in ?

---

<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: [30 January 2022 11:52 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/16 "2022-01-30T11:52:37Z")

</div>

> [@janedoe123](#):
>
> Sorry for asking again about the Mask, how can I use it to extract only the 33-bit that I am interested in

You have to find the right position in the byte stream. then use a 33 bit mask based on the data. If necessary, you might have to shift the data to right align. Depending on the data format, you may also need to byteswap

PS: 33 bit mask would be `0x00000001FFFFFFFF` assuming the data is starting in a sensible position  
Another 33 bit mask would be `0x000001FFFFFFFF00` if it was left shifted 1 digit/nibble (4 bits) etc  
And IF the data is misaligned then after getting the value you would need to right shift it to correct it e.g. `value = (value >> 4);`

Are you certain those 2 sample data, 33 bits from bit 175 have the value `2653870640`? The sample data is completely different!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f881278c20ac0a2f2a13ff764892300318b16734.png)

If you can provide sample data and the actual expected value, I'll help you solve this.

---

<div class="post-metadata">

### Author: ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)
#### Post date: [30 January 2022 13:23 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/19 "2022-01-30T13:23:29Z")

</div>

Hi @Steve-Mcl

I just got it working, but what a mess ! I literally had to do trial and errors, and had to do three things:

- enabled swap64
- used bigint64 (LE)
- and used your magical Mask

And obviously, I'm using the v3.2.2 🙂

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/7629accaaa13619baaf86e1b1e53828054f94432.png)

Thank you so much !!

---

<div class="post-metadata">

### Author: ![janedoe123](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@janedoe123](https://discourse.nodered.org/u/janedoe123)
#### Post date: [31 January 2022 00:41 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/21 "2022-01-31T00:41:10Z")

</div>

Hi @Steve-Mcl

Little up 🙂

---

<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: [31 January 2022 09:03 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/22 "2022-01-31T09:03:29Z")

</div>

Hi.

I cant really make head nor sense of the data layout. I do know if you choose offset 17 you can read the 45000 value you are expecting.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f8318a85eb3821ef82a7cc52d4f4c591e54de708.png)

I wonder if the data is out by 1 byte or the details are incorrect or the data is double swapped?

I say that because bit 175 is actually the last bit (first bit LSB) of byte 21.  
Assuming the 33 bit value is packed in 64 bits then the data would layout as follows...

| byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | **21** | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| value | 252 | 48 | 32 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 255 | 240 | 15 | 5 | 97 | 211 | 135 | 180 | 127 | 255 | 126 | 0 | 6 | 221 | 208 | 0 | 56 | 0 | 0 | 0 | 0 | 12 | 195 | 15 | 166 |
| 1st bit | 0 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 | 88 | 96 | 104 | 112 | 120 | 128 | 136 | 144 | 152 | 160 | 168 | 176 | 184 | 192 | 200 | 208 | 216 | 224 | 232 | 240 | 248 | 256 | 264 | 272 |
| last bit | 7 | 15 | 23 | 31 | 39 | 47 | 55 | 63 | 71 | 79 | 87 | 95 | 103 | 111 | 119 | 127 | 135 | 143 | 151 | 159 | 167 | **175** | 183 | 191 | 199 | 207 | 215 | 223 | 231 | 239 | 247 | 255 | 263 | 271 | 279 |
| byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | **21** | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 |
| MASK | | | | | | | | | | | | | | | | | | | 00 | 00 | 00 | 01 | FF | FF | FF | FF | | | | | | | | | |

* * *

Dont you have any more detailed documentation - often manuals have a sample data, data type descriptions and a worked example showing raw data + final value.

---

<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: [18 February 2022 10:38 UTC](https://discourse.nodered.org/t/how-to-read-a-33-bit-field-with-node-red-contrib-binary/57483/23 "2022-02-18T10:38:32Z")

</div>

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