# Decoding the binary array with buffer parser

**URL:** <https://discourse.nodered.org/t/decoding-the-binary-array-with-buffer-parser/82388>\
**Category:** General\
**Created:** [27 October 2023 09:03 UTC](https://discourse.nodered.org/t/decoding-the-binary-array-with-buffer-parser/82388 "2023-10-27T09:03:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rnodist](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rnodist](https://discourse.nodered.org/u/rnodist)\
**Post date:** [27 October 2023 09:03 UTC](https://discourse.nodered.org/t/decoding-the-binary-array-with-buffer-parser/82388/1 "2023-10-27T09:03:39Z")

</div>

Hello, would need a bit help here to understand this thing.  
I have a battery device, which sends it's data on tcp port, every second.  
Protocol documentation says:

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

I managed to connect with TCP node to that port and get data (stream of Buffer payloads). But now I don't understand how to decode that data to correct values.

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

Data, which I receive from TCP node, looks like this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c0e9a806b624ed39189d27f256096a6125645108.png)

For example Cells temperature I tried with buffer-parser node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/b/ebb228020c05c63d56a91b35d6cf2e694e870bf4.png)

and the result is array:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6eece5f95110cc89c46779bd7ebd9d119c25687.png)

What should I do to get out value like temperature from that?

---

<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:** [27 October 2023 09:52 UTC](https://discourse.nodered.org/t/decoding-the-binary-array-with-buffer-parser/82388/2 "2023-10-27T09:52:35Z")

</div>

The size of the data in your image is 4 bytes therefore it is a 32bit number.

So you set the data type to match (probably int32be/int32le or floatBe/floatLe depending on how it is packed), set the offset to the correct position and length to 1 to "get one 32bit value".

As the built in help states:

> **length** The quantity of items to be returned. e.g. 6 bools or 12 floats or 34 int32s.

---

<div class="post-metadata">

**Author:** ![rnodist](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rnodist](https://discourse.nodered.org/u/rnodist)\
**Post date:** [27 October 2023 11:52 UTC](https://discourse.nodered.org/t/decoding-the-binary-array-with-buffer-parser/82388/3 "2023-10-27T11:52:15Z")

</div>

I copied one buffer with real data and that monent system own app showed temperature 8.12°c (or 8.06°c, it was changing).

I still can not understand, how I can not get correct value. I tried all types you mentioned.

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

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

```auto
[
    {
        "id": "15b0d39fdc1eff30",
        "type": "inject",
        "z": "2c55f2a82c964ec5",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[50,55,218,161,216,175,4,16,0,200,70,0,0,0,0,0,0,0,0,136,255,36,0,0,0,146,39,1,0,0,0,40,0,0,146,39,0,0,0,0,40,0,53,146,39,44,3,0,0,40,0,56,146,39,0,0,0,0,40,0,54]",
        "payloadType": "bin",
        "x": 770,
        "y": 820,
        "wires": [
            [
                "12465585669b7f18",
                "4f5fdfc58b75118d"
            ]
        ]
    },
    {
        "id": "12465585669b7f18",
        "type": "buffer-parser",
        "z": "2c55f2a82c964ec5",
        "name": "",
        "data": "payload",
        "dataType": "msg",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "type": "floatle",
                "name": "cellsTemp",
                "offset": 46,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            }
        ],
        "swap1": "",
        "swap2": "",
        "swap3": "",
        "swap1Type": "swap",
        "swap2Type": "swap",
        "swap3Type": "swap",
        "msgProperty": "payload",
        "msgPropertyType": "str",
        "resultType": "value",
        "resultTypeType": "return",
        "multipleResult": true,
        "fanOutMultipleResult": false,
        "setTopic": true,
        "outputs": 1,
        "x": 950,
        "y": 820,
        "wires": [
            [
                "34fdb5fee2db7896"
            ]
        ]
    },
    {
        "id": "34fdb5fee2db7896",
        "type": "debug",
        "z": "2c55f2a82c964ec5",
        "name": "debug 236",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1130,
        "y": 820,
        "wires": []
    },
    {
        "id": "4f5fdfc58b75118d",
        "type": "debug",
        "z": "2c55f2a82c964ec5",
        "name": "debug 237",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 950,
        "y": 880,
        "wires": []
    }
]

```

---

<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:** [27 October 2023 12:50 UTC](https://discourse.nodered.org/t/decoding-the-binary-array-with-buffer-parser/82388/4 "2023-10-27T12:50:38Z")

</div>

> [@rnodist](#):
>
> I still can not understand, how I can not get correct value. I tried all types you mentioned.

There are many reasons why you may not be getting sane values:

- the data has padding at the start (e.g. you data starts at byte 47 or 48)?
- the data may NOT be an IEEE 754 32 bit float (some devices make up their own float format)
- the data may simply be an INT32 and require you to divide by 100 or 1000
- the data may actually be Fahrenheit or kelvin and your APP converts it to Celcius.
- the whole data string might be byte-swapped

Do you have a manual - perhaps it explains how values are packed into the data stream?

All I can suggest is capture several buffers of data, put them into inject nodes (like you did), setup a buffer parser with multiple entries ALL pointed to the **same start byte** BUT with different "types" (e.g. item1: int32be, item2: int32le, item3: float32be, item4: float32le) - see if you can spot `8.06` or `806` or `8060000` etc). See if you can spot something close.

If that doesnt work, Try changing the byteswap parameter. Try changing the byte offset by 1. etc etc.

---

<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:** [26 December 2023 12:50 UTC](https://discourse.nodered.org/t/decoding-the-binary-array-with-buffer-parser/82388/5 "2023-12-26T12:50:49Z")

</div>

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