Howto merge two hex buffer values in node-red

0

I have a UDP (multicast) input node. It receives every second a 600 Byte buffer with hex values. The searched value is stored at positions msg.payload[33] to msg.payload[36]. I have to combine these four hex values to one hex value and convert this to decimal. For example:

msg.payload[33]: 0x00
msg.payload[34]: 0x00
msg.payload[35]: 0x02
msg.payload[36]: 0x3a

Now merge these four hex values to 0x0000023a and convert it to decimal, which is 570.

I tried a lot with change and join nodes, but nothing worked as expected. Any ideas on that? I think a function node would help, but I have no coding experience with that. Any hints?

Best regards!

Edit: That is the original msg.payload after the udp input node:

20.1.2019, 19:08:25node: 8b0e1675.1c6dc8
msg.payload : buffer[600]
buffer[600]raw
[0 … 9]
[10 … 19]
[20 … 29]
[30 … 39]
30: 0x4
31: 0x0
32: 0x0
33: 0
34: 0x1c
35: 0xfb
36: 0
37: 1
38: 0x8
39: 0x0
[40 … 49]

and so on until 600.

node-red
asked on Stack Overflow Jan 18, 2019 by Conti • edited Jan 20, 2019 by Conti

1 Answer

0

node-red-contrib-binary will do this for you, you will need to add a pattern that matches the format of the whole buffer. The syntax for the pattern can be found here.

You would end up with something like:

... value: b32, ...

it would then output a msg.payload.value with the number.

I a function node assuming you passed the buffer in as msg.paylaod it would be something like this:

var value = msg.paylaod.readInt32BE(33);

EDIT:

If the input is just a short array then you can do something like the following in a function node:

var value = (mag.payload[0] << 8) + (mag.payload[1] << 24)
    + (msg.payload[2] << 16) + (msg.payload[3] << 8);

Or just convert the array to a buffer and read as before:

var buff = Buffer.from(msg.payload);
var value = buff.readInt32BE(0);
answered on Stack Overflow Jan 18, 2019 by hardillb • edited Jan 20, 2019 by hardillb

User contributions licensed under CC BY-SA 3.0