# Convert decimal to hex then store in binary buffer

**URL:** https://discourse.nodered.org/t/convert-decimal-to-hex-then-store-in-binary-buffer/84435
**Category:** General
**Created:** [10 January 2024 01:52 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-then-store-in-binary-buffer/84435 "2024-01-10T01:52:50Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)
#### Post date: [10 January 2024 01:52 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-then-store-in-binary-buffer/84435/1 "2024-01-10T01:52:50Z")

</div>

Hello

I have a slider node that I slide from 0 to 40. The output is obviously a decimal number.

I need to convert this to hex, so I used var hexstring1 = "0x" + msg.payload.toString(16);

However, when I try to load this value into a binary buffer, say msg.payload = Buffer.from(0x00, [hexstring1], 0x00, 0x01);, I get an error.

How do I convert the decimal number so I can store it in the buffer?

I do have other values I am setting in the buffer, which are numbers.

---

<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: [10 January 2024 07:47 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-then-store-in-binary-buffer/84435/2 "2024-01-10T07:47:49Z")

</div>

> [@Sirhc](#):
>
> Buffer.from(0x00, [hexstring1], 0x00, 0x01);

Just put the number in there

Buffer.from([0x00, msg.payload, 0x00, 0x01]);

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [10 January 2024 07:50 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-then-store-in-binary-buffer/84435/3 "2024-01-10T07:50:45Z")

</div>

> [@Steve-Mcl](#):
>
> Buffer.from(0x00, msg.payload, 0x00, 0x01);

Shouldn't this be

```auto
Buffer.from([0x00, msg.payload, 0x00, 0x01]);

```

?

---

<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: [10 January 2024 07:52 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-then-store-in-binary-buffer/84435/4 "2024-01-10T07:52:04Z")

</div>

Yes indeed. Written from phone/untested/pre-coffee) 😉

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [10 January 2024 07:52 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-then-store-in-binary-buffer/84435/5 "2024-01-10T07:52:54Z")

</div>

> [@Steve-Mcl](#):
>
> pre-coffee

Never do! High risc action! 😉

---

<div class="post-metadata">

### Author: ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)
#### Post date: [11 January 2024 20:27 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-then-store-in-binary-buffer/84435/6 "2024-01-11T20:27:03Z")

</div>

Thank you!

---

<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: [11 March 2024 20:27 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-then-store-in-binary-buffer/84435/7 "2024-03-11T20:27:31Z")

</div>

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