How to convert array byte to ASCII


#1

Guys Happy new year!

I working in my device and need to decode a array byte to ASCII stream, like you can see below:

image

I using let param1 = msg.payload[28], for example to get a byte.
I need to create a variable that will have within the word derived from the byte sequence of the buffer.

in my PIC above, as example, the array bytes becomes in the word RWL020.
0x52, 0x57, 0x4c, 0x30, 0x32, 0x30 => RWL020

Anybody can help, please?

Best Regards,
Alex


#2

add this to a function node and see if it works

msg.payload = Buffer.from(msg.payload);
msg.payload = msg.payload.toString();
return msg;

#3

Hi zenofmud,
I did it but is not working, as you can see in part of my code below:

if(msg.payload[26]!=2){		
	
}else{
																						let a = msg.payload[27];
let attriblengthb2 = (a^0x10).toString(16).padStart(2,"0");
}
let stream = msg.payload[28].toString();
msg.payload = Buffer.from(msg.payload[28]);
msg.payload = msg.payload.toString();
return msg;
																									//let str = String.fromCharCode(parseInt(stream.substr(i, 2), 16));
																									//return str;
																							}

image

I tried to use also ~~~ String.fromCharCode(parseInt(stream.substr(i, 2), 16)); ~~~
But is not working also. You can see PIC below:

image

Do you have any idea how to fix it?


#4

Please attach a debug node - set to display the complete msg object - to the node generating the array. Then copy and past the debug output.

Also - try commenting out all your code in the function so just the lies i gave you remain - what result do you get?


#5

Could be done with this code in a function node:

msg.payload = String.fromCharCode(0x52, 0x57, 0x4c, 0x30, 0x32, 0x30);
return msg;

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode


#6

Hi Paul,
As you requested me, you can see below the debug message error after i comment out my code:

image

Best Regards,
Alex


#7

Hi Andrei,
thanks.
now is working well to get one byte.

my code using buffer component:

let stream = msg.payload[32];		
msg.payload = String.fromCharCode(stream);
return msg;

image

I would like to create a word, as example RWL020, from the specific buffer positions.I have a byte that content the byte stream length, Would you can advise me the best way to do it?


#8

@aargollo I wanted to see the data going into the function which is why I asked to connect the debug to the node generating the array.


#9

In such case fallback to @zenofmud solution, :joy:

The only point is that you need to know the start and the end for the extraction. Only knowing how many bytes to extract from the buffer won't be useful, right?

I amended Paul´s solution to include your latest requirement. You can test code below inside a function node. Use different values for start and end for the testing. I am assuming you will input in the function a buffer inside msg.payload

function sliceBuf(buf, start, end) {
    return buf.toString("ascii").slice(start,end);
}

msg.sliced = sliceBuf(msg.payload,21,25);
return msg;


#10

Hi Paul and Zenofmud!
Working well like Swiss clock! :slight_smile:
Thanks you for the help you and Zenofmud gave me!

Best Regards
Alex