Buffer to "string"

Hi,

I'm currently working on parsing my payload on node red. So to summarize I have the following payload (which is a buffer): [70,224,0,0,1,140,0,0,0,0]

And I'm looking for a way / a function to transform it in a string like that: 702240011400000

Do you guys know a function (or a node) which can do the work ? I can't find ..

Thanks in advance,
Regards,
Cédric R

Are you sure that is what you want to do? That seems a very odd thing to want. What are you going to do with it as a string?

Hi,

Yes, I know this is not unusual (and maybe that is why I am having a hard time finding a solution) but I am 100% sure this is what I need to accomplish.
Currently I'm translating the buffer to base64, but it's not correct for the person I'm working with.

He absolutely wants the payload to be in hexadecimal and also in JSON (and buffers are not recognized in json).

So about the solution, I think I'll try to do loop which append each term of the buffer to a string. I don't know if it's will work, let's see !

I have found a solution:

Let's take a simple example

Here is msg.payload: [10,0,255]
the equivalent buffer in hex is: 0xa 0x0 0xff

What I'm trying to do is to have a JSON like this:

{   
"deviceid": "ABC0000BCA",
"value": "0A 00 FF", // A concatened string of the hex buffer
"devicetype": "HomeDetection"
}

So to achieve this, I used a function to transform [0xa 0x0 0xff] in a string like this: 0A 00 FF:

// Creation of a function to get the Hex Value from a message
function* hexFormatValues(buffer) {
  for (let x of buffer) {
    const hex = x.toString(16)
    yield hex.padStart(2, '0')
  }
}

// Variable declaration for my loop
var string = ""
const buf = Buffer.from(msg.payload)

// Concatenation of the buffer in a string 
for (let hex of hexFormatValues(buf)) {
  string = string + hex + " " // You can remove the " " if you don't want spaces between the bytes
} 

msg.payload = string;
return msg;

And in the JSON template I put the following:

{   
"deviceid": "ABC0000BCA",
"value": {{payload}}, // A concatened string of the hex buffer
"devicetype": "HomeDetection"
}

I'm not sure this will ever be of use to anyone but I am sharing the solution just in case. :slight_smile:

Regards

1 Like

To get 702240011400000 from a buffer of [70,224,0,0,1,140,0,0,0,0], just join them...

Buffer.from([70,224,0,0,1,140,0,0,0,0]).join("");
//outputs: 702240011400000

image


But, as your 2nd post infers, you actually want HEX so that is done like this...

Buffer.from([70,224,0,0,1,140,0,0,0,0]).toString("hex");
//outputs: 46e00000018c00000000

image


And lastly, since you seem to want upper case HEX bytes with spaces between every byte...

Buffer.from([70,224,0,0,1,140,0,0,0,0]).toString("hex").toUpperCase().replace( /(?<=^(.{2})+)(?!$)/g, ' ' );
//outputs: 46 E0 00 00 01 8C 00 00 00 00

1 Like

That is not what you asked for in your question.

Hi Steve,

You are right, I just noticed that I had made a mistake in the expression of my initial request, thank you for noticing it.

Also, the method you use seems much simpler and lighter than mine, I'll try that.

Thank you for your sharing

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