# Convert HEX) to Decimal

**URL:** <https://discourse.nodered.org/t/convert-hex-to-decimal/27750>\
**Category:** General\
**Created:** [4 June 2020 07:07 UTC](https://discourse.nodered.org/t/convert-hex-to-decimal/27750 "2020-06-04T07:07:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![droidblast](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@droidblast](https://discourse.nodered.org/u/droidblast)\
**Post date:** [4 June 2020 07:07 UTC](https://discourse.nodered.org/t/convert-hex-to-decimal/27750/1 "2020-06-04T07:07:38Z")

</div>

Need to covert EnergyToday (HEX) to Decimal. What is the best way to do this?

```auto
<response>
<EnergyToday>00BB</EnergyToday>
<EnergyYesterday>02EC</EnergyYesterday>
<EnergyLifetime>00283FDE</EnergyLifetime>
<HeaterInstallEnergy>00000000</HeaterInstallEnergy>
<HoursExportedToday>424</HoursExportedToday>
<HoursExportedYesterday>489</HoursExportedYesterday>
<HoursExportedLifetime>00118017</HoursExportedLifetime>
<DaysProducing>06CC</DaysProducing>
<HeaterInstalled>0000</HeaterInstalled>
<InsolationToday>0000</InsolationToday>
<InsolationYesterday>0000</InsolationYesterday>
<MeterToday>00000000</MeterToday>
<MeterYesterday>0000</MeterYesterday>
<MeterLifetime>00000000</MeterLifetime>
<HeaterToday>0000</HeaterToday>
<HeaterYesterday>0000</HeaterYesterday>
<HeaterLifetime>00000000</HeaterLifetime>
<ExportToday>0000</ExportToday>
<ExportYesterday>0000</ExportYesterday>
<ExportLifetime>00000000</ExportLifetime>
<ImportToday>0000</ImportToday>
<ImportYesterday>0000</ImportYesterday>
<ImportLifetime>00000000</ImportLifetime>
</response>

```

---

<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:** [4 June 2020 07:15 UTC](https://discourse.nodered.org/t/convert-hex-to-decimal/27750/2 "2020-06-04T07:15:16Z")

</div>

Pass the XML through an XML node to create a JS object then you could access the object properties in a function node & convert their values to numbers using parseInt.

---

<div class="post-metadata">

**Author:** ![droidblast](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@droidblast](https://discourse.nodered.org/u/droidblast)\
**Post date:** [4 June 2020 07:17 UTC](https://discourse.nodered.org/t/convert-hex-to-decimal/27750/3 "2020-06-04T07:17:43Z")

</div>

> [@Steve-Mcl](#):
>
> p

Thanks thats the part I can get right, need to add the fn node after the change node but the code is killing me.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/4/e48792ec2be603b3276d6fd4a37571a11e97e7d3.png)

---

<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:** [4 June 2020 07:20 UTC](https://discourse.nodered.org/t/convert-hex-to-decimal/27750/4 "2020-06-04T07:20:15Z")

</div>

What's your function code?

---

<div class="post-metadata">

**Author:** ![droidblast](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@droidblast](https://discourse.nodered.org/u/droidblast)\
**Post date:** [4 June 2020 07:26 UTC](https://discourse.nodered.org/t/convert-hex-to-decimal/27750/5 "2020-06-04T07:26:23Z")

</div>

`msg.payload = parseInt("0x"+msg.payload);`

this works but changes `Preformatted text`00BB to 187 where I want 1.87

Can add the calulator node and divide by 100 but there must be a better way?

---

<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:** [4 June 2020 07:30 UTC](https://discourse.nodered.org/t/convert-hex-to-decimal/27750/6 "2020-06-04T07:30:45Z")

</div>

00bb is 187

However, you want a different value, divide by 100 or use the scale node.

```auto
msg.payload = parseInt("0x"+msg.payload) / 10.0;
return msg;

```

However from my original suggestion above, I would have done this....

DATA \> XML NODE (convert data to js object) \> FUNCTION NODE (convert all values in one place)

---

<div class="post-metadata">

**Author:** ![droidblast](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@droidblast](https://discourse.nodered.org/u/droidblast)\
**Post date:** [4 June 2020 07:31 UTC](https://discourse.nodered.org/t/convert-hex-to-decimal/27750/7 "2020-06-04T07:31:31Z")

</div>

Thanks will revise as per your advice....

---

<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:** [3 August 2020 07:31 UTC](https://discourse.nodered.org/t/convert-hex-to-decimal/27750/8 "2020-08-03T07:31:33Z")

</div>

Closed on 3 August 2020 07:31 UTC

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