How to concat a buffer


#1

Hi Guys, I would like to concat a buffer but i got the follow error:

I injecting in the function the follow buffer:

["0x01","0x80","0x49","0x02","0x10","0x02","0x13","0xbc","0x22","0x02","0x10","0x54","0x03"]

Blockquote

function : (error)

Blockquote

"TypeError: "list" argument must be an Array of Buffer or Uint8Array instances"

here is my code:

Blockquote

switch (msg.payload[2]){
//Analyze Device Annoucement
case 73:{ // device announcement message 0x43
var a,b;
var c = [];
a = msg.payload[9]; // analyze nwk short address [9] and [10]
b = msg.payload[10];
c= [a,b];
// Buffer concat arguments
var msg1 = { payload: new Buffer.concat(c)};
return msg1;

}

Blockquote
Somebody can help me to fix it?

Best Regards,
Alex


#2

Please can you fix the formatting of your post to properly display the code? It is hard to help debug code that isn't formatted properly.


#3

Hi knolleary!
I tried to do it. Please let me know if is ok for you right now?


#4

Hi Guys, I got success to concatenate two array components as a string using JOIN node. I didn´t know at this moment if is the best way but it worked.

I got buffer component for each function in the flow before join node using a follow code:

Blockquote
for (i= 0; i < msg.payload.length; i++) {

  switch (msg.payload[2]){
        //Analyze Device Annoucement 
       case 73:{ // device announcement message 
              var buf1 =[];
              buf1 = msg.payload[9]; // analyze nwk short address 

        var msg1 = { payload: buf1};
        return msg1;

       }
    break;  

}


#5

Hi Guys, i got success to concatenate but i´ll need to working with a hex values, as you can see my buffer have 0xcd and 0xfd at buffer position 9 and 10, but when I see that in the debug node the values appear in decimal, since my functions before the join node convert it to hex using parseInt.

my decimal components buffer is:
[1,2,16,77,2,16,2,28,103,205,253,2,16,23,136,2,17,2,18,26,131,232,128,123,3]

image

my debug log always it it in decimal base:

image

Somebody can help me to fix it?
I would like to have in debug log the cdfd hex value!


#6

Hi Alex,

It would be easier to help you if you format correctly the code you post in the forum.

You can do so by adding three backtick characters before the code and another set of three at the end of the code. If you can´t find it your keyboard it is ok, you can use the tilde character instead.

You don´t need to post again the code. It is better that you edit your previous posts to correct. This helps to keep the thread clean and useful for future reference.

```
a = msg.payload[9]; // analyze nwk short address [9] and [10]
b = msg.payload[10];
```

or

~~~
a = msg.payload[9]; // analyze nwk short address [9] and [10]
b = msg.payload[10];
~~~

will result

a = msg.payload[9]; // analyze nwk short address [9] and [10]
b = msg.payload[10];

#7

If you created it as a number type rather than a string then you can click on it in the debug sidebar and it will show you it in decimal or hex.

PS 0xcdfd is 52733 in decimal... so something isn't correct.


#8

@dceejay, thanks a lot, I´ll store it as a variable for use in another function node. I´ll check if the two bytes will persist in hex when I´ll store in a global variable, file or DB.

I found my wrong code, I used ParseInt improperly. I´ll try to explain better my need.
I need to format a parameter that have a role formation
Nwk Addr > msg.payload[9] concatenated with msg.payload[10].

for example: in a buffer received by my parser function have: 205 dec (cd hex), at position 9 and 253 dec (fd hex) at position 10. My Nwk addr should be: cdfd.

after I have withdrawn ParseInt my nwk addr persists as 205253. You can see below my actually code:

Function: DevAnn - ShortAddrPart1 The 2st function (ShortAddPart2) have the same code, but changes from msg.payload[9] to msg.payload[10].

for (i= 0; i < msg.payload.length; i++) {

// handling parameters and send a response mesage
      switch (msg.payload[3]){
            //Analyze Device Annoucement 
           case 77:{ // device announcement message 
              //var buf1 =[];
              //buf1 = parseInt (msg.payload[9],16); // analyze nwk short address
              var buf1;
              buf1 = msg.payload[9];
              var msg1 = { payload: buf1};
              return msg1;

           }
        break;  


        }

My Join node configuration:

@Andrei,
I finally wrote the right code.. Thanks a lot for your help!

@dceejay, do you can help to keep my nwk addr as a hex?


#9

Alex,

Let me get this straight: you want to input the binary buffer in the flow and get as result a string representation of bytes 9 and 10, right ?

In such case, for the given binary buffer the expected output would be "cdfd".

If the understanding is correct then the coding is quite simple if you use the Javascript method toString. Check here: https://www.w3schools.com/jsref/jsref_tostring_number.asp

This is the code to use in your function:

msg.add = msg.payload[9].toString(16) + msg.payload[10].toString(16);
return msg;

Result:

r-01