# Convert HEX to useful values?

**URL:** <https://discourse.nodered.org/t/convert-hex-to-useful-values/58620>\
**Category:** General\
**Created:** [18 February 2022 19:19 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620 "2022-02-18T19:19:37Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [24 February 2022 18:03 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/21 "2022-02-24T18:03:47Z")

</div>

How you have created these strings in your second posting (4th post)?

> [@Convert HEX to useful values?](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/4):
>
> Ok, now I get decimal or integer values ... they do not mean anything but ok, first step done slight_smile The serial input looks like this: 24 24 57 0F 0E 24 01 00 E6 00 81 00 84 5B 27 24 24 57 0F 0E 24 01 00 E4 00 81 00 84 5B 25 24 24 57 0F 0E 24 01 00 E1 00 83 00 84 5B 24 24 24 56 2D 0C FD 0D 04 0D 04 0D 02 0D 03 0D 04 0D 06 0D 01 0D 08 0D 02 0D 05 0C FE 0D 06 0C FB 0D 0F 0C FC 76 FE D5 02 63 14 0E 00 95 24 24 58 28 01 E4 00 01 00 03 00 03 00 03 00 02 00 03 00 00 00 00 00 01 00 01 00 …

What is the output when you use the standard settings:

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

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [24 February 2022 18:11 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/22 "2022-02-24T18:11:10Z")

</div>

in this case I get rubbish symbols in strings with different lengths:

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

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [24 February 2022 18:20 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/23 "2022-02-24T18:20:39Z")

</div>

I tried your solution to re-arragent the buffer but there is something broken with the offset ... I do not understand this part good enough to fix it myself ☹

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

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [24 February 2022 18:26 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/24 "2022-02-24T18:26:19Z")

</div>

Can you change the fist buffer maker as follows:

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

remove the link first to the first buffer parser - and post the debug of the buffer maker output

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

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

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [24 February 2022 18:30 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/25 "2022-02-24T18:30:00Z")

</div>

the result is a set of 5 messages with buffers of different lengths ... continously repaeted

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

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [24 February 2022 18:32 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/26 "2022-02-24T18:32:46Z")

</div>

That looks perfect . so the first string should be filtered out by the complete flow. - so in my opinion you can try the flow.

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

This looks to match the correct scheme.

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [24 February 2022 18:33 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/27 "2022-02-24T18:33:22Z")

</div>

sorry, no ... it is even more complicated:

the length of the answer-string is quite variable:

5, 15, 16, 76 and 77 characters .... sometimes (seldom) only 66

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

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [24 February 2022 18:35 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/28 "2022-02-24T18:35:17Z")

</div>

So the compete flow should filter out the strings which starts with 36,36,86  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f074ad4869e846aa9579daf3bb7deac481a9ea5b.png)

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

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [24 February 2022 18:40 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/29 "2022-02-24T18:40:49Z")

</div>

After the puffer parser node I get A LOT of messages containing the wanted pattern ... but not on the right position:

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

most of the time they are ignored:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/4/c4168abe0bcf717c7ac8b8e4274bb206061deb20.png)

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [24 February 2022 18:52 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/30 "2022-02-24T18:52:43Z")

</div>

OK - so in principle it is working but we need somehow splitting the stream at the correct positions.

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [24 February 2022 18:52 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/31 "2022-02-24T18:52:53Z")

</div>

could it be that /n is the wrong character to split the stream ?

these strings are FAR to short:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a18510cd7e8211824b4eaa4e554ed2a0c1e96c76.png)

thats how the green ones start and they are MUCH longer ...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3fda2be4a29a0c55220613937a638e3a9cf41ba3.png)

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [24 February 2022 19:01 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/32 "2022-02-24T19:01:37Z")

</div>

Can look what the result would be if you split a buffer with 0x24

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

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [24 February 2022 19:07 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/33 "2022-02-24T19:07:05Z")

</div>

even more strange things 🙂

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

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [24 February 2022 19:22 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/34 "2022-02-24T19:22:03Z")

</div>

So I have now an idea how to get it probably correct - separated.

can you use your serial node in that way - that single bytes will be the output (no character specified)

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

or buffer = 0

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

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [24 February 2022 19:24 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/35 "2022-02-24T19:24:43Z")

</div>

output as expected:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4d61952aa0e490e5c1838db42d5592c79123e2dc.png)

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [24 February 2022 19:30 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/36 "2022-02-24T19:30:34Z")

</div>

So unfortunately is the split node not working as expected. In my opinion an issue should be raised.

Originally I expected that the split node would work to analyse the data stream:

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

**However the split not works not properly** - so the data stream is splitted each time a byte with value 36 is recognized - and not the whole buffer means 2 bytes with value 36.

So I wrote 2 function nodes - the first node splits a stream into parts splitted by 2 bytes - defined as header:

```auto
const header = Buffer.from([0x24,0x24]);

```

The second function node filters all messages in combination of header and command.

```auto
const filter = Buffer.from([0x24,0x24,0x56]);

```

So I hope the flow will work:

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

```auto
[
    {
        "id": "868aa4256ca689ac",
        "type": "function",
        "z": "78d94accf7f5ec3c",
        "name": "Analyse stream",
        "func": "if (msg.flush){\n msg.payload=context.get(\"buffer\");\n context.set(\"buffer\",undefined);\n return msg;\n}\n\n\nconst header = Buffer.from([0x24,0x24]);\nvar buffer = context.get(\"buffer\")||Buffer.alloc(0);\n\nvar arr = [buffer, msg.payload];\n\nmsg.payload = undefined;\nbuffer = Buffer.concat(arr);\n\n\nif (buffer.indexOf(header) >= 0 ) {\n var tmp = buffer.slice(2);\n var arrSliced =[]; \n \n var i = tmp.indexOf(header);\n\n if (tmp.indexOf(header) >= 0 ){ \n arrSliced[0] = buffer.slice(0, i + 2);\n buffer = buffer.slice(i + 2); \n if (arrSliced[0].indexOf(header) >= 0) msg.payload = arrSliced[0];\n if (msg.payload.equals(header)) msg.payload = \"\";\n }\n \n}\n\ncontext.set(\"buffer\",buffer);\nif (msg.payload) return msg;\n \n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 2120,
        "y": 4160,
        "wires": [
            [
                "4ba58e3fe27b80d3"
            ]
        ]
    },
    {
        "id": "4ba58e3fe27b80d3",
        "type": "function",
        "z": "78d94accf7f5ec3c",
        "name": "filter command",
        "func": "const filter = Buffer.from([0x24,0x24,0x56]);\nif (Buffer.isBuffer(msg.payload)) {\n if (filter.equals(msg.payload.slice(0,3))) return msg;\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 2320,
        "y": 4160,
        "wires": [
            [
                "850b1c253ed01d58",
                "623dcd793312299e"
            ]
        ]
    },
    {
        "id": "57f673a1708bf489",
        "type": "debug",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 2690,
        "y": 4160,
        "wires": []
    },
    {
        "id": "850b1c253ed01d58",
        "type": "buffer-parser",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "data": "payload",
        "dataType": "msg",
        "specification": "spec",
        "specificationType": "ui",
        "items": [
            {
                "type": "byte",
                "name": "Header",
                "offset": 0,
                "length": 2,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "hex",
                "name": "Command",
                "offset": 2,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "uint8",
                "name": "DataLength",
                "offset": 3,
                "length": 1,
                "offsetbit": 0,
                "scale": "1",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell1",
                "offset": 4,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell2",
                "offset": 6,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell3",
                "offset": 8,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell4",
                "offset": 10,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell5",
                "offset": 12,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell6",
                "offset": 14,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell7",
                "offset": 16,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell8",
                "offset": 18,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell9",
                "offset": 20,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell10",
                "offset": 22,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell11",
                "offset": 24,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell12",
                "offset": 26,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell13",
                "offset": 28,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell14",
                "offset": 30,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell15",
                "offset": 32,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint16be",
                "name": "Cell16",
                "offset": 34,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint32le",
                "name": "Wh",
                "offset": 36,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint32le",
                "name": "Ah",
                "offset": 40,
                "length": 1,
                "offsetbit": 0,
                "scale": "/1000",
                "mask": ""
            },
            {
                "type": "uint8",
                "name": "CheckSum",
                "offset": 44,
                "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": 2510,
        "y": 4160,
        "wires": [
            [
                "57f673a1708bf489"
            ]
        ]
    },
    {
        "id": "4ccf16891a5588e8",
        "type": "inject",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[36,36,86,45,12,253,13,4,13,4,13,2,13,3,13,4,13,6,13,1,13,8,13,2,13,5,12,254,13,6,12,251,13,15,12,252,118,254,213,2,99,20,14,0,149]",
        "payloadType": "bin",
        "x": 2130,
        "y": 4200,
        "wires": [
            [
                "4ba58e3fe27b80d3"
            ]
        ]
    },
    {
        "id": "c4f48b5d708eb501",
        "type": "inject",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "props": [
            {
                "p": "flush",
                "v": "true",
                "vt": "bool"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 1900,
        "y": 4200,
        "wires": [
            [
                "868aa4256ca689ac"
            ]
        ]
    },
    {
        "id": "623dcd793312299e",
        "type": "debug",
        "z": "78d94accf7f5ec3c",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 2530,
        "y": 4120,
        "wires": []
    }
]

```

One supplement:

As the buffers of the analyse node are sent only when the next header is detected - the last buffer remains in the context. To flush this buffer a msg.flush with a boolean true has to be sent to empty the node. So an additional trigger node is recommended, when the stream of bytes stopped.

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [25 February 2022 07:15 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/37 "2022-02-25T07:15:09Z")

</div>

It worked at the very first try 🥳 🥳 🥳

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

---

<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:** [11 March 2022 07:15 UTC](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620/38 "2022-03-11T07:15:39Z")

</div>

Closed on 11 March 2022 07:15 UTC

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

[Previous page](https://discourse.nodered.org/t/convert-hex-to-useful-values/58620.md?page=1)
