How to convert a BASE64 -> Hex -> decimal

Hi,
I am querying various measurements from a Modbus device that transmits its data back via LoRaWan -> MQTT -> NodeRed as a Base64 string.

With the Base64 -> Hex Node I can already display the hex values. The display is shown per hex value in a single line. But now I don't know how to extract individual values and convert them into dec.

I have attached the code below and I have made two text examples for the calculation game...

[
    {
        "id": "83f1c874b550c374",
        "type": "inject",
        "z": "f242f543b9ed7515",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "CxQAHgBB6SlcQsiDCUD1NjRB6e80Qr27ikDnoCYBLw+g",
        "payloadType": "str",
        "x": 550,
        "y": 220,
        "wires": [
            [
                "8e57c9275d6b6e11"
            ]
        ]
    },
    {
        "id": "8e57c9275d6b6e11",
        "type": "base64",
        "z": "f242f543b9ed7515",
        "name": "",
        "action": "",
        "property": "payload",
        "x": 760,
        "y": 220,
        "wires": [
            [
                "812a65a321541581"
            ]
        ]
    },
    {
        "id": "812a65a321541581",
        "type": "debug",
        "z": "f242f543b9ed7515",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 950,
        "y": 220,
        "wires": []
    }
]

Example with mesage.payload: "CxQAHgBB6SlcQsiDCUD1NjRB6e80Qr27ikDnoCYBLw+g"
equals: 0b1400 1e00 41e9 295c 42c8 8309 40f5 3634 41e9 ef34 42bd bb8a 40e7 a026 012f 0fa0

0b1400 - 3 byte informations from the database
1E00 - 2 byte informations about the device
41E9 B9BD - 2 byte value 1
42C7 2083 - 2 byte value 2
40F3 356B - 2 byte value 3
41EA 8AA0 - 2 byte value 4
42BD 498C - 2 byte value 5
40E6 C413 - 2 byte value 6
0130 - 1 byte value 7
0FA1 - 1 byte value 8

Example with mesage.payload: "C0kABgABMg+g"
equals: 0b4900 0600 0132 0fa0

0b4900 - 3 byte informations from the database
0600 - 2 byte informations about the device
0132 - 1 byte value 1
0fa0 - 1 byte value 2

  • 41E9 B9BD << This is 4 bytes not 2 bytes!. What value do you expect 41E9 B9BD be as decimal?
  • 42C7 2083 << This is 4 bytes not 2 bytes!. What value do you expect 42C7 2083 be as decimal?
  • 40F3 356B << This is 4 bytes not 2 bytes!. What value do you expect 40F3 356B be as decimal?
  • 41EA 8AA0 << This is 4 bytes not 2 bytes!. What value do you expect 41EA 8AA0 be as decimal?
  • 42BD 498C << This is 4 bytes not 2 bytes!. What value do you expect 42BD 498C be as decimal?
  • 40E6 C413 << This is 4 bytes not 2 bytes!. What value do you expect 40E6 C413 be as decimal?
  • 0130 << This is 2 bytes not 1 byte. What value do you expect 0130 be as decimal?
  • 0FA1 << This is 2 bytes not 1 byte. What value do you expect 0FA1 be as decimal?

Sorry, sure... double the amount of bytes... for example the last 2 byte value OFA1 equals 4001. Thats the number i would like to have as a msg.payload. I build a example flow in the moment. Will be uploaded in 5min...

[
    {
        "id": "83f1c874b550c374",
        "type": "inject",
        "z": "f242f543b9ed7515",
        "name": "CxQAHgBB6SlcQsiDCUD1NjRB6e80Qr27ikDnoCYBLw+g",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "CxQAHgBB6SlcQsiDCUD1NjRB6e80Qr27ikDnoCYBLw+g",
        "payloadType": "str",
        "x": 550,
        "y": 240,
        "wires": [
            [
                "8e57c9275d6b6e11"
            ]
        ]
    },
    {
        "id": "8e57c9275d6b6e11",
        "type": "base64",
        "z": "f242f543b9ed7515",
        "name": "",
        "action": "",
        "property": "payload",
        "x": 860,
        "y": 240,
        "wires": [
            [
                "ba04ea6d626b1114"
            ]
        ]
    },
    {
        "id": "a26f2f9f6d5ba56f",
        "type": "debug",
        "z": "f242f543b9ed7515",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1290,
        "y": 240,
        "wires": []
    },
    {
        "id": "ba04ea6d626b1114",
        "type": "change",
        "z": "f242f543b9ed7515",
        "name": "",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": ",",
                "fromt": "str",
                "to": "",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1080,
        "y": 240,
        "wires": [
            [
                "a26f2f9f6d5ba56f"
            ]
        ]
    },
    {
        "id": "dc1977260b0f99ef",
        "type": "function",
        "z": "f242f543b9ed7515",
        "name": "",
        "func": "msg.payload = parseInt(\"0x\"+msg.payload);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 780,
        "y": 520,
        "wires": [
            [
                "ae332aca663b1e1c"
            ]
        ]
    },
    {
        "id": "04902d8a5e5f51b6",
        "type": "inject",
        "z": "f242f543b9ed7515",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "40E6 C413",
        "payloadType": "str",
        "x": 560,
        "y": 600,
        "wires": [
            [
                "dc1977260b0f99ef"
            ]
        ]
    },
    {
        "id": "ae332aca663b1e1c",
        "type": "debug",
        "z": "f242f543b9ed7515",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 950,
        "y": 520,
        "wires": []
    },
    {
        "id": "30dea182fd20cf1a",
        "type": "inject",
        "z": "f242f543b9ed7515",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "42BD 498C",
        "payloadType": "str",
        "x": 570,
        "y": 560,
        "wires": [
            [
                "dc1977260b0f99ef"
            ]
        ]
    },
    {
        "id": "7e7b68b5bbe448d4",
        "type": "inject",
        "z": "f242f543b9ed7515",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "41E9 B9BD",
        "payloadType": "str",
        "x": 570,
        "y": 400,
        "wires": [
            [
                "dc1977260b0f99ef"
            ]
        ]
    },
    {
        "id": "8b2b102bb409083c",
        "type": "inject",
        "z": "f242f543b9ed7515",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "42C7 2083",
        "payloadType": "str",
        "x": 560,
        "y": 440,
        "wires": [
            [
                "dc1977260b0f99ef"
            ]
        ]
    },
    {
        "id": "65cbaeb88349ecb9",
        "type": "inject",
        "z": "f242f543b9ed7515",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "40F3 356B",
        "payloadType": "str",
        "x": 560,
        "y": 480,
        "wires": [
            [
                "dc1977260b0f99ef"
            ]
        ]
    },
    {
        "id": "4be9b6038a6352a6",
        "type": "inject",
        "z": "f242f543b9ed7515",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "41EA 8AA0",
        "payloadType": "str",
        "x": 570,
        "y": 520,
        "wires": [
            [
                "dc1977260b0f99ef"
            ]
        ]
    },
    {
        "id": "fe71e4f5302395fe",
        "type": "inject",
        "z": "f242f543b9ed7515",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0130 ",
        "payloadType": "str",
        "x": 550,
        "y": 640,
        "wires": [
            [
                "dc1977260b0f99ef"
            ]
        ]
    },
    {
        "id": "1ef23bfb98061766",
        "type": "inject",
        "z": "f242f543b9ed7515",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0FA1 ",
        "payloadType": "str",
        "x": 550,
        "y": 680,
        "wires": [
            [
                "dc1977260b0f99ef"
            ]
        ]
    },
    {
        "id": "c7c90032307dc0ea",
        "type": "comment",
        "z": "f242f543b9ed7515",
        "name": "4 byte value 1",
        "info": "",
        "x": 370,
        "y": 400,
        "wires": []
    },
    {
        "id": "f6801133cf12c432",
        "type": "comment",
        "z": "f242f543b9ed7515",
        "name": "4 byte value 1",
        "info": "",
        "x": 370,
        "y": 440,
        "wires": []
    },
    {
        "id": "9e40b79b426dbcbb",
        "type": "comment",
        "z": "f242f543b9ed7515",
        "name": "4 byte value 1",
        "info": "",
        "x": 370,
        "y": 480,
        "wires": []
    },
    {
        "id": "e5657ddb75dde1df",
        "type": "comment",
        "z": "f242f543b9ed7515",
        "name": "4 byte value 1",
        "info": "",
        "x": 370,
        "y": 520,
        "wires": []
    },
    {
        "id": "9b6710eb69eff307",
        "type": "comment",
        "z": "f242f543b9ed7515",
        "name": "4 byte value 1",
        "info": "",
        "x": 370,
        "y": 560,
        "wires": []
    },
    {
        "id": "897f5fc896a2bc59",
        "type": "comment",
        "z": "f242f543b9ed7515",
        "name": "4 byte value 1",
        "info": "",
        "x": 370,
        "y": 600,
        "wires": []
    },
    {
        "id": "e9c2093990df9c80",
        "type": "comment",
        "z": "f242f543b9ed7515",
        "name": "4 byte value 1",
        "info": "",
        "x": 370,
        "y": 640,
        "wires": []
    },
    {
        "id": "552826457b8c6015",
        "type": "comment",
        "z": "f242f543b9ed7515",
        "name": "4 byte value 1",
        "info": "",
        "x": 370,
        "y": 680,
        "wires": []
    },
    {
        "id": "45564ab60ac6ca09",
        "type": "comment",
        "z": "f242f543b9ed7515",
        "name": "Base64 example 1",
        "info": "",
        "x": 210,
        "y": 240,
        "wires": []
    },
    {
        "id": "e4c9c31dc5ca96cd",
        "type": "comment",
        "z": "f242f543b9ed7515",
        "name": "HEX example 1",
        "info": "",
        "x": 200,
        "y": 400,
        "wires": []
    },
    {
        "id": "99290d7d64e7f913",
        "type": "comment",
        "z": "f242f543b9ed7515",
        "name": "I would like to automate this output as the HEX examples below",
        "info": "",
        "x": 1450,
        "y": 280,
        "wires": []
    }
]

node-red-contrib-buffer-parser is what you need.

Example using...
BASE64: CxQAHgBB6SlcQsiDCUD1NjRB6e80Qr27ikDnoCYBLw+g
BYTES: 0b14001e0041e9295c42c8830940f5363441e9ef3442bdbb8a40e7a026012f0fa0

Demo flow...

[{"id":"83f1c874b550c374","type":"inject","z":"ec9cf53f5ff54f3b","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"CxQAHgBB6SlcQsiDCUD1NjRB6e80Qr27ikDnoCYBLw+g","payloadType":"str","x":1850,"y":200,"wires":[["8e57c9275d6b6e11"]]},{"id":"8e57c9275d6b6e11","type":"base64","z":"ec9cf53f5ff54f3b","name":"","action":"","property":"payload","x":1980,"y":200,"wires":[["c4672580a42399d7","36f98126726186c6"]]},{"id":"c4672580a42399d7","type":"buffer-parser","z":"ec9cf53f5ff54f3b","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"hex","name":"database","offset":0,"length":3,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"device","offset":3,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint32be","name":"value1","offset":7,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint32be","name":"value2","offset":11,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint32be","name":"value3","offset":15,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint32be","name":"value4","offset":19,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint32be","name":"value5","offset":23,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint32be","name":"value6","offset":25,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"value7","offset":29,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"value8","offset":31,"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":1990,"y":260,"wires":[["812a65a321541581"]]},{"id":"812a65a321541581","type":"debug","z":"ec9cf53f5ff54f3b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2170,"y":260,"wires":[]},{"id":"36f98126726186c6","type":"debug","z":"ec9cf53f5ff54f3b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2170,"y":200,"wires":[]}]
3 Likes

awesome! Thanks!

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