# Convert decimal to hex and back after transformation

**URL:** https://discourse.nodered.org/t/convert-decimal-to-hex-and-back-after-transformation/33280
**Category:** General
**Created:** [24 September 2020 10:52 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-and-back-after-transformation/33280 "2020-09-24T10:52:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![John245](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@John245](https://discourse.nodered.org/u/John245)
#### Post date: [24 September 2020 10:52 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-and-back-after-transformation/33280/1 "2020-09-24T10:52:39Z")

</div>

I want to do the following.

msg.payload[8]=12  
msg.payload[9]=39161

I want to convert msg.payload[8] and [9] to Hex numbers (respectively C and 98F9).

I have several issues.

1. how do I convert to Hex numbers
2. how do I convert from Hex to decimal. I tried  
var HexNumber = 'C98F9';  
msg.payload = parseInt(HexNumber);  
return msg;  
But that resulted in NaN

0xF will result in 15

I'm a newbie in Node-Red, any help is appreciated.

* * *

John

---

<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: [24 September 2020 11:05 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-and-back-after-transformation/33280/2 "2020-09-24T11:05:39Z")

</div>

When you say you want to convert it to a hex number, do you mean that you want a string containing "98f9"? If you are not sure what that means then tell us what do you want to do with the hex number.

---

<div class="post-metadata">

### Author: ![John245](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@John245](https://discourse.nodered.org/u/John245)
#### Post date: [24 September 2020 11:17 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-and-back-after-transformation/33280/3 "2020-09-24T11:17:07Z")

</div>

I want to convert both payloads to Hex and then combine both Hex numbers and convert to a decimal number

So If I can convert the payloads to Hex (resulting in C and 98FG) and combine these strings (resulting in C98FG) it is ok. After that the string containing the Hex number should be converted to a decimal value (end result 825593).

* * *

John

---

<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: [24 September 2020 11:35 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-and-back-after-transformation/33280/4 "2020-09-24T11:35:56Z")

</div>

You don't need to convert it at all, just shift the first value left 16 bits and add on the second

```auto
msg.payload = (msg.payload[8] <<16) + (msg.payload[9] & 0xffff)
return msg

```

The and with 0xffff is to force it to an unsigned value, as, for example, if the second word was -1 it would actually mean 65535

---

<div class="post-metadata">

### Author: ![John245](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@John245](https://discourse.nodered.org/u/John245)
#### Post date: [24 September 2020 12:21 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-and-back-after-transformation/33280/5 "2020-09-24T12:21:01Z")

</div>

Thanks a lot, this is much simpler as in C++.

* * *

John

---

<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: [23 November 2020 12:21 UTC](https://discourse.nodered.org/t/convert-decimal-to-hex-and-back-after-transformation/33280/6 "2020-11-23T12:21:05Z")

</div>

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