# Convert hex string to float

**URL:** https://discourse.nodered.org/t/convert-hex-string-to-float/28893
**Category:** General
**Created:** [24 June 2020 14:35 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893 "2020-06-24T14:35:45Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![steifanc](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@steifanc](https://discourse.nodered.org/u/steifanc)
#### Post date: [24 June 2020 14:35 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/1 "2020-06-24T14:35:45Z")

</div>

Hi,

This is probably pretty rudimentary but I can't seem to get it right. I have a JSON object containing a HEX value, im trying to change it to a float/decimal it's a temperature reading.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/7/07ca885ebc9f718779e652348dd68899a082923a.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: [24 June 2020 14:47 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/2 "2020-06-24T14:47:41Z")

</div>

What format is it in? There are many ways of representing float values. If you don't know the format where did it come from?

---

<div class="post-metadata">

### Author: ![steifanc](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@steifanc](https://discourse.nodered.org/u/steifanc)
#### Post date: [24 June 2020 15:13 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/3 "2020-06-24T15:13:04Z")

</div>

Hi,  
Its a float value from a tm35 sensor converted to hex and sent as a json message to node red,

---

<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: [24 June 2020 15:30 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/4 "2020-06-24T15:30:10Z")

</div>

Hi

Without context its fairly meaningless.

For example is it a scaled value?  
Is it in engineering units or raw from the sensor?  
What number are you expecting 0x32352e3339 (dec 215640585017) to represent in Celsius?

---

<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 June 2020 15:40 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/5 "2020-06-24T15:40:06Z")

</div>

> [@steifanc](#):
>
> converted to hex

Also how has it been converted to hex (and why)? Why not just send the float value?

---

<div class="post-metadata">

### Author: ![steifanc](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@steifanc](https://discourse.nodered.org/u/steifanc)
#### Post date: [24 June 2020 16:36 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/6 "2020-06-24T16:36:23Z")

</div>

Hi,  
The temp sensor is value is say 25.39 as a float. The temp is converted to ascii symbol and then to hex, 2(32), 5(35), .(2e), 3(33), 9(39) giving 32352e3339 this is send as raw to the sigfox backend, watson IOT, and to node red as a JSON object. I'm trying now to get the JSON object back to the float 25.39

---

<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: [24 June 2020 16:41 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/7 "2020-06-24T16:41:58Z")

</div>

That would have been useful info for the first post!  
You can use a function node

e.g.

```auto
msg.payload = Buffer.from(msg.d,"hex").toString()
return msg;

```

Some proof...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/eff184713f7d6cbaa198ff5c96ffb75bb0a0c83e.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: [24 June 2020 17:02 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/8 "2020-06-24T17:02:30Z")

</div>

@steifanc to explain why your question was not clear, if, for example, you were getting float values via modbus from some device then they might well be IEEE754 encoded which is quite different, and there are other possibilities too.

---

<div class="post-metadata">

### Author: ![steifanc](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@steifanc](https://discourse.nodered.org/u/steifanc)
#### Post date: [25 June 2020 08:05 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/9 "2020-06-25T08:05:01Z")

</div>

Thanks all ,  
the function is throwing up a type error , ill need to play with it,  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e3763e24afe77e5e45fbc75bd8cf3db4137b9aed.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: [25 June 2020 08:20 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/10 "2020-06-25T08:20:51Z")

</div>

show us the debug output from before the function node and the function node code.

I suspect you are not getting the right property from the msg or its a number not a string.

All are easy fixes.

---

<div class="post-metadata">

### Author: ![steifanc](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@steifanc](https://discourse.nodered.org/u/steifanc)
#### Post date: [25 June 2020 08:43 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/11 "2020-06-25T08:43:49Z")

</div>

Hi ,

Function node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c54f34660464d0440c88c9702b2db355b51cb5cd.png)

Debug node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/28d064cfdd27191bf7ae2597363afd8c36d31cd1.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: [25 June 2020 08:54 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/12 "2020-06-25T08:54:31Z")

</div>

so where is the part that converts from hex to ascii?

```auto
msg.payload = Buffer.from(msg.d,"hex").toString()

```

In particular - I need to see what you are using inside the Buffer.from function and I need to see what it contains (a string? a number?)

The error is pretty clear (first arg must be a string)

---

<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: [25 June 2020 08:58 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/13 "2020-06-25T08:58:44Z")

</div>

Also in that function you are referencing `msg.payload.d.value` but the debug output you show has msg.payload as a string not an object with attribute `d`  
I think it would be worth your while reading the node red docs page Working with Messages to get some background information on how to handle messages.

---

<div class="post-metadata">

### Author: ![steifanc](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@steifanc](https://discourse.nodered.org/u/steifanc)
#### Post date: [25 June 2020 09:08 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/14 "2020-06-25T09:08:07Z")

</div>

Hi ,  
I don`t have a buffer function.  
I have a message coming from watson IOT , below and im trying to change the message back to the float value

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e10abf9c39bb5a80f23729749889067c3550f5a3.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: [25 June 2020 09:15 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/15 "2020-06-25T09:15:57Z")

</div>

> [@steifanc](#):
>
> I don`t have a buffer function.  
> I have a message coming from watson IOT , below and im trying to change the message back to the float value

So you basically ignored the solution provided...

> [@Steve-Mcl](#):
>
> You can use a function node
> 
> e.g.
> 
> ```auto
> msg.payload = Buffer.from(msg.d,"hex").toString()
> return msg;
> 
> ```
> 
> Some proof...  
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/eff184713f7d6cbaa198ff5c96ffb75bb0a0c83e.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: [25 June 2020 09:17 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/16 "2020-06-25T09:17:18Z")

</div>

Post your flow and I'll fix it for you

---

<div class="post-metadata">

### Author: ![steifanc](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@steifanc](https://discourse.nodered.org/u/steifanc)
#### Post date: [25 June 2020 09:29 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/17 "2020-06-25T09:29:01Z")

</div>

Hi ,  
I did try the function you provided and i got the Type error.

flow below

```auto
[{"id":"c81a5aed.869158","type":"debug","z":"4dc36eba.fc14d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1110,"y":540,"wires":[]},{"id":"e94d6a10.4b4fc8","type":"function","z":"4dc36eba.fc14d","name":"","func":"msg.payload = Buffer.from(msg.d,\"hex\").toString()\nreturn msg;","outputs":1,"noerr":0,"x":910,"y":460,"wires":[["c8bb370a.08bef8"]]},{"id":"1a909fde.c739d","type":"function","z":"4dc36eba.fc14d","name":"","func":"p=msg.payload;\nnode.log(typeof p);\nmsg.payload=p.d.value;\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":460,"wires":[["e94d6a10.4b4fc8","c81a5aed.869158"]]},{"id":"c8bb370a.08bef8","type":"debug","z":"4dc36eba.fc14d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1090,"y":460,"wires":[]},{"id":"188f638b.05a33c","type":"ibmiot in","z":"4dc36eba.fc14d","authentication":"apiKey","apiKey":"8b357272.1f43","inputType":"evt","logicalInterface":"","ruleId":"","deviceId":"36C664","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"value","service":"registered","allDevices":false,"allApplications":"","allDeviceTypes":true,"allLogicalInterfaces":"","allEvents":true,"allCommands":"","allFormats":false,"qos":0,"x":590,"y":460,"wires":[["1a909fde.c739d"]]},{"id":"8b357272.1f43","type":"ibmiot","z":"","name":"Test_","keepalive":"60","serverName":"","cleansession":true,"appId":"","shared":false}]

```

---

<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: [25 June 2020 09:36 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/18 "2020-06-25T09:36:29Z")

</div>

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

```auto
[{"id":"46f2e36d.79332c","type":"debug","z":"8712f97c.b302e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1110,"y":100,"wires":[]},{"id":"5c3a833c.59b90c","type":"function","z":"8712f97c.b302e8","name":"","func":"msg.payload = Buffer.from(msg.payload.d,\"hex\").toString()\nreturn msg;","outputs":1,"noerr":0,"x":1110,"y":160,"wires":[["b874a36c.dd7ef"]]},{"id":"b874a36c.dd7ef","type":"debug","z":"8712f97c.b302e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1290,"y":160,"wires":[]},{"id":"f1adb05f.c5deb","type":"inject","z":"8712f97c.b302e8","name":"","topic":"fake ibmiot","payload":"{\"d\":\"32352e3339\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":900,"y":100,"wires":[["5c3a833c.59b90c","46f2e36d.79332c"]]},{"id":"1cf837b5.8c22b8","type":"ibmiot in","z":"8712f97c.b302e8","authentication":"apiKey","apiKey":"8b357272.1f43","inputType":"evt","logicalInterface":"","ruleId":"","deviceId":"36C664","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"value","service":"registered","allDevices":false,"allApplications":"","allDeviceTypes":true,"allLogicalInterfaces":"","allEvents":true,"allCommands":"","allFormats":false,"qos":0,"x":870,"y":160,"wires":[["5c3a833c.59b90c"]]}]

```

ignore the missing node (I dont use that node)

---

<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: [25 June 2020 09:50 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/19 "2020-06-25T09:50:56Z")

</div>

Looking at the debug it seems to be `msg.payload.d.value`.

@steifanc have a read through the node red docs page Working with Messages. Understanding that I think you would have been able to modify @Steve-Mcl's suggestion to match your data.

---

<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: [25 June 2020 10:02 UTC](https://discourse.nodered.org/t/convert-hex-string-to-float/28893/20 "2020-06-25T10:02:22Z")

</div>

Good spot.

Updated the demo flow to use msg.payload.d.value...

```auto
[{"id":"46f2e36d.79332c","type":"debug","z":"8712f97c.b302e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1110,"y":100,"wires":[]},{"id":"5c3a833c.59b90c","type":"function","z":"8712f97c.b302e8","name":"","func":"msg.payload = Buffer.from(msg.payload.d.value,\"hex\").toString()\nreturn msg;","outputs":1,"noerr":0,"x":1110,"y":160,"wires":[["b874a36c.dd7ef"]]},{"id":"b874a36c.dd7ef","type":"debug","z":"8712f97c.b302e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1290,"y":160,"wires":[]},{"id":"f1adb05f.c5deb","type":"inject","z":"8712f97c.b302e8","name":"","topic":"fake ibmiot","payload":"{\"d\":{\"value\":\"32352e3339\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":830,"y":100,"wires":[["5c3a833c.59b90c","46f2e36d.79332c"]]},{"id":"1cf837b5.8c22b8","type":"ibmiot in","z":"8712f97c.b302e8","authentication":"apiKey","apiKey":"8b357272.1f43","inputType":"evt","logicalInterface":"","ruleId":"","deviceId":"36C664","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"value","service":"registered","allDevices":false,"allApplications":"","allDeviceTypes":true,"allLogicalInterfaces":"","allEvents":true,"allCommands":"","allFormats":false,"qos":0,"x":870,"y":160,"wires":[["5c3a833c.59b90c"]]}]

```

[Next page](https://discourse.nodered.org/t/convert-hex-string-to-float/28893.md?page=2)
