# Buffer to "string"

**URL:** <https://discourse.nodered.org/t/buffer-to-string/45276>\
**Category:** General\
**Created:** [6 May 2021 14:46 UTC](https://discourse.nodered.org/t/buffer-to-string/45276 "2021-05-06T14:46:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Archi](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Archi](https://discourse.nodered.org/u/Archi)\
**Post date:** [6 May 2021 14:46 UTC](https://discourse.nodered.org/t/buffer-to-string/45276/1 "2021-05-06T14:46:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 May 2021 15:49 UTC](https://discourse.nodered.org/t/buffer-to-string/45276/2 "2021-05-06T15:49:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Archi](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Archi](https://discourse.nodered.org/u/Archi)\
**Post date:** [7 May 2021 09:31 UTC](https://discourse.nodered.org/t/buffer-to-string/45276/3 "2021-05-07T09:31:51Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![Archi](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Archi](https://discourse.nodered.org/u/Archi)\
**Post date:** [7 May 2021 09:58 UTC](https://discourse.nodered.org/t/buffer-to-string/45276/4 "2021-05-07T09:58:58Z")

</div>

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:

```auto
{   
"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** :

```auto
// 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:

```auto
{   
"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. 🙂

Regards

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [7 May 2021 12:32 UTC](https://discourse.nodered.org/t/buffer-to-string/45276/5 "2021-05-07T12:32:56Z")

</div>

> [@Archi](#):
>
> 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**

  

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

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

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/356dd52406f9cbffb4a97f869a821d9a0e6236b4.png)

  

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

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

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ac491832886f5fbda1a83f32be93df2ba78b5328.png)

  

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

```javascript
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

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/1688715d5253448d1cef6c812838e95fe32c284a.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 May 2021 13:00 UTC](https://discourse.nodered.org/t/buffer-to-string/45276/6 "2021-05-07T13:00:45Z")

</div>

> [@Archi](#):
>
> 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:
> 
> ```auto
> {   
> "deviceid": "ABC0000BCA",
> "value": "0A 00 FF",
> 
> ```

That is not what you asked for in your question.

---

<div class="post-metadata">

**Author:** ![Archi](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Archi](https://discourse.nodered.org/u/Archi)\
**Post date:** [7 May 2021 13:10 UTC](https://discourse.nodered.org/t/buffer-to-string/45276/7 "2021-05-07T13:10:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 May 2021 13:11 UTC](https://discourse.nodered.org/t/buffer-to-string/45276/8 "2021-05-21T13:11:10Z")

</div>

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