# Setup Decimal/Hex to binary for buffer-parser

**URL:** <https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933>\
**Category:** General\
**Tags:** function-node\
**Created:** [21 December 2023 17:34 UTC](https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933 "2023-12-21T17:34:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [21 December 2023 17:34 UTC](https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933/1 "2023-12-21T17:34:01Z")

</div>

I'm having trouble converting a single decimal to binary and then parsing it with buffer-parser.  
Let say I have decimal number 256. I'm converting that to a binary string and then into a buffer.

```auto
var payload = msg.payload.toString(2);
msg.payload = Buffer.from(payload, "utf-8");
return msg;

```

My buffer = 100000000

The issue i'm having is Buffer-parser sets bit 0 as true instead of bit 8.

```auto
[true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false]

```

I'm expecting a result with bit 8 as true.  
Is there a proper way to get this binary data into buffer-parser?

---

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [21 December 2023 22:29 UTC](https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933/2 "2023-12-21T22:29:05Z")

</div>

So this is what I've come up with so far but it seems kind of convoluted.  
Also I'm having trouble getting a number out of bits 3-7.

```auto
[
    {
        "id": "f49454a0393f7fa0",
        "type": "inject",
        "z": "d03c03b6d2f91f47",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "256",
        "payloadType": "num",
        "x": 190,
        "y": 1580,
        "wires": [
            [
                "24264fdf5894afce"
            ]
        ]
    },
    {
        "id": "57da651af048c75b",
        "type": "debug",
        "z": "d03c03b6d2f91f47",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 790,
        "y": 1580,
        "wires": []
    },
    {
        "id": "24264fdf5894afce",
        "type": "function",
        "z": "d03c03b6d2f91f47",
        "name": "function 11",
        "func": "var payload = (msg.payload.toString(2));\npayload = ([...payload].reverse().join(\"\")).split(\"\")\nvar delay = 250; //I shortened your delay\nvar ticker = \"0\";\nvar array = payload\ndo {\n\n if (payload.length < 21) {\n array.push(ticker);\n\n }\n}\nwhile (payload.length < 21)\n\nmsg.buffer = array\n\nmsg.payload = (Buffer.from(payload, \"binary\"));\n\n\n\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 1580,
        "wires": [
            [
                "f8f80d3e98422312"
            ]
        ]
    },
    {
        "id": "f8f80d3e98422312",
        "type": "buffer-parser",
        "z": "d03c03b6d2f91f47",
        "name": "",
        "data": "payload",
        "dataType": "msg",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "type": "bool",
                "name": "first_message_after_reset",
                "offset": 0,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "user_button_triggered",
                "offset": 1,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "user_BLE_triggered",
                "offset": 2,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "buffer",
                "name": "retries",
                "offset": 3,
                "length": 5,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "bool",
                "name": "Rule1",
                "offset": 8,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "Rule2",
                "offset": 9,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "Rule3",
                "offset": 10,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "Rule4",
                "offset": 11,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "Rule5",
                "offset": 12,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "Rule6",
                "offset": 13,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "Rule7",
                "offset": 14,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "Rule8",
                "offset": 15,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "Rule9",
                "offset": 16,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "Rule10",
                "offset": 17,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "Rule11",
                "offset": 18,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "Rule12",
                "offset": 19,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            },
            {
                "type": "bool",
                "name": "end_of_transfer_limit",
                "offset": 20,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": "1"
            }
        ],
        "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": 610,
        "y": 1580,
        "wires": [
            [
                "57da651af048c75b"
            ]
        ]
    }
]

```

 ![2023-12-21_15-28-39](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5a4740606a2fdc9b3d22f2035fb042f42f2cfd6.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:** [21 December 2023 23:34 UTC](https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933/3 "2023-12-21T23:34:57Z")

</div>

The issue is to do with byte swap / system architecture etc.

Here is how you can handle it in 3 different ways:

![chrome_ODhvuI4Bar](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd7e1673c3f66483a4ea31b93501f98a9fd30819.gif)

Demo flow (use CTRL+I to import)

```auto
[{"id":"c0c9a6eb1e5e3741","type":"inject","z":"3642c7ee286f9c17","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"256","payloadType":"num","x":930,"y":240,"wires":[["c630ef9b82fb7076"]]},{"id":"7dc24905850871f4","type":"inject","z":"3642c7ee286f9c17","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0xffff","payloadType":"num","x":930,"y":280,"wires":[["c630ef9b82fb7076"]]},{"id":"1eb2b4a8b16bc141","type":"inject","z":"3642c7ee286f9c17","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0xffff","payloadType":"num","x":930,"y":320,"wires":[["c630ef9b82fb7076"]]},{"id":"cfe562a89211d5bb","type":"inject","z":"3642c7ee286f9c17","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0x0001","payloadType":"num","x":930,"y":360,"wires":[["c630ef9b82fb7076"]]},{"id":"e30ed63f943e6fee","type":"inject","z":"3642c7ee286f9c17","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0x1010","payloadType":"num","x":930,"y":400,"wires":[["c630ef9b82fb7076"]]},{"id":"c630ef9b82fb7076","type":"function","z":"3642c7ee286f9c17","name":"convert to binary string","func":"msg.payload = msg.payload.toString(2)\nnode.status({fill:\"green\",shape:\"ring\",text:msg.payload});\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1160,"y":240,"wires":[["93bced855e0e73a4"]]},{"id":"328233c3ef9c4825","type":"inject","z":"3642c7ee286f9c17","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":930,"y":200,"wires":[["c630ef9b82fb7076"]]},{"id":"0c6c639e3a507495","type":"inject","z":"3642c7ee286f9c17","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0100000000000001","payloadType":"str","x":970,"y":480,"wires":[["93bced855e0e73a4"]]},{"id":"93bced855e0e73a4","type":"function","z":"3642c7ee286f9c17","name":"make array of integer","func":"msg.payload = [parseInt(msg.payload, 2)]\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1280,"y":360,"wires":[["6cfc68412109badd","9f6df1413604e97e","61e23145977342b4","4a5d79cc1ed16f21"]]},{"id":"6cfc68412109badd","type":"debug","z":"3642c7ee286f9c17","name":"debug 2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1520,"y":340,"wires":[]},{"id":"9f6df1413604e97e","type":"buffer-parser","z":"3642c7ee286f9c17","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"item1","offset":0,"length":16,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":1,"x":1530,"y":280,"wires":[["8d599a2e66f170ce"]]},{"id":"61e23145977342b4","type":"buffer-parser","z":"3642c7ee286f9c17","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bool","name":"item1","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"bool","name":"item2","offset":0,"length":1,"offsetbit":1,"scale":"1","mask":""},{"type":"bool","name":"item3","offset":0,"length":1,"offsetbit":2,"scale":"1","mask":""},{"type":"bool","name":"item4","offset":0,"length":1,"offsetbit":3,"scale":"1","mask":""},{"type":"bool","name":"item5","offset":0,"length":1,"offsetbit":4,"scale":"1","mask":""},{"type":"bool","name":"item6","offset":0,"length":1,"offsetbit":5,"scale":"1","mask":""},{"type":"bool","name":"item7","offset":0,"length":1,"offsetbit":6,"scale":"1","mask":""},{"type":"bool","name":"item8","offset":0,"length":1,"offsetbit":7,"scale":"1","mask":""},{"type":"bool","name":"item9","offset":0,"length":1,"offsetbit":8,"scale":"1","mask":""},{"type":"bool","name":"item10","offset":0,"length":1,"offsetbit":9,"scale":"1","mask":""},{"type":"bool","name":"item11","offset":0,"length":1,"offsetbit":10,"scale":"1","mask":""},{"type":"bool","name":"item12","offset":0,"length":1,"offsetbit":11,"scale":"1","mask":""},{"type":"bool","name":"item13","offset":0,"length":1,"offsetbit":12,"scale":"1","mask":""},{"type":"bool","name":"item14","offset":0,"length":1,"offsetbit":13,"scale":"1","mask":""},{"type":"bool","name":"item15","offset":0,"length":1,"offsetbit":14,"scale":"1","mask":""},{"type":"bool","name":"item16","offset":0,"length":1,"offsetbit":15,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":16,"x":1530,"y":520,"wires":[["4d0e30f6ca5310c9"],["fdd64a21a999302d"],["e0611d46579115da"],["bead61f1a7f2db92"],[],[],[],[],[],[],[],[],["b162b7a3d0036820"],["6c499f3a8673119c"],["40fb07335a3636c3"],["0ec37d50fc7475c9"]]},{"id":"4d0e30f6ca5310c9","type":"debug","z":"3642c7ee286f9c17","name":"debug 3","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1740,"y":340,"wires":[]},{"id":"fdd64a21a999302d","type":"debug","z":"3642c7ee286f9c17","name":"debug 4","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1740,"y":400,"wires":[]},{"id":"e0611d46579115da","type":"debug","z":"3642c7ee286f9c17","name":"debug 5","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1740,"y":460,"wires":[]},{"id":"bead61f1a7f2db92","type":"debug","z":"3642c7ee286f9c17","name":"debug 6","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1740,"y":520,"wires":[]},{"id":"b162b7a3d0036820","type":"debug","z":"3642c7ee286f9c17","name":"debug 7","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1740,"y":600,"wires":[]},{"id":"6c499f3a8673119c","type":"debug","z":"3642c7ee286f9c17","name":"debug 8","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1740,"y":660,"wires":[]},{"id":"40fb07335a3636c3","type":"debug","z":"3642c7ee286f9c17","name":"debug 9","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1740,"y":720,"wires":[]},{"id":"0ec37d50fc7475c9","type":"debug","z":"3642c7ee286f9c17","name":"debug 10","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1740,"y":780,"wires":[]},{"id":"8d599a2e66f170ce","type":"debug","z":"3642c7ee286f9c17","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1740,"y":280,"wires":[]},{"id":"97ba6e0bbe601b66","type":"inject","z":"3642c7ee286f9c17","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1111000000000000","payloadType":"str","x":970,"y":520,"wires":[["93bced855e0e73a4"]]},{"id":"4a5d79cc1ed16f21","type":"buffer-parser","z":"3642c7ee286f9c17","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"16bitle","name":"item1","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":true,"setTopic":true,"outputs":1,"x":1530,"y":220,"wires":[["087a7e7180d15f80"]]},{"id":"087a7e7180d15f80","type":"debug","z":"3642c7ee286f9c17","name":"debug 12","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1740,"y":220,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [22 December 2023 15:02 UTC](https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933/4 "2023-12-22T15:02:17Z")

</div>

Hi Steve, thanks very much for the example. I should have included in my first post that my source is a uint32 integer.  
So for example 1048832.  
I'm struggling getting this into a format that will work with 32 byte swap.

---

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [22 December 2023 15:23 UTC](https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933/5 "2023-12-22T15:23:11Z")

</div>

This seems to work.

```auto
msg.payload = msg.payload.toString(2).padStart(32, "0");
msg.payload = (Buffer.from(msg.payload, "utf8"));

return msg;

```

\*I take that back. I just got lucky with number 1048832.

---

<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:** [22 December 2023 15:44 UTC](https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933/6 "2023-12-22T15:44:27Z")

</div>

We should go back to the beginning.

Do you actually need to be converting bit strings?

What is the source data - a 32 bit UINT?

---

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [22 December 2023 15:49 UTC](https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933/7 "2023-12-22T15:49:05Z")

</div>

The source is from a CoAP payload. Processed through a proto buff with this information provided.

```auto
  /* Reason of transmission - unsigned integer where each bit indicates different possible communication reason. */
  /* Can be more than one: */
  /* - bit 0: first message after sensor reset */
  /* - bit 1: user button triggered */
  /* - bit 2: user BLE triggered */
  /* - bit 3-7: number of retries -> incremented after each unsuccessful transmission. Max value 31. */
  /* Set to 0 after a successful transmission. */
  /* - bit 8...19: rule 1...12 was met */
  /* - bit 20: triggered after the end of the limit */
  uint32 transfer_reason = 6;

```

---

<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:** [22 December 2023 16:00 UTC](https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933/8 "2023-12-22T16:00:49Z")

</div>

Send it through a buffer maker (1 row, uint32) to generate a buffer , pass that to buffer parser (use the example I posted earlier)

---

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [22 December 2023 17:27 UTC](https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933/9 "2023-12-22T17:27:33Z")

</div>

Ah it's always so simple in the end. Thanks again.

---

<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:** [5 January 2024 17:27 UTC](https://discourse.nodered.org/t/setup-decimal-hex-to-binary-for-buffer-parser/83933/10 "2024-01-05T17:27:49Z")

</div>

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