# Object to float value

**URL:** <https://discourse.nodered.org/t/object-to-float-value/47849>\
**Category:** General\
**Created:** [30 June 2021 16:51 UTC](https://discourse.nodered.org/t/object-to-float-value/47849 "2021-06-30T16:51:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikingsraven](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vikingsraven/32/49243_2.png) [@vikingsraven](https://discourse.nodered.org/u/vikingsraven)\
**Post date:** [30 June 2021 16:51 UTC](https://discourse.nodered.org/t/object-to-float-value/47849/1 "2021-06-30T16:51:36Z")

</div>

HI, im trying to get my head round javascript still, ground to a halt again, and it will probably be a really easy thing to resolve, ive tried googling and not had any luck.  
I have a serial device that is send battery data, certain parts are binary data others are ascii depenedant on the message and the address, this i have sorted and i have the appropiate channels switching to there resepctive channels.  
now to the point, the payload im getting out is a

```auto
buffer  
 [73,81,70,80,84]

```

, now this is showing as decimal numbers, but if i open the array they are HEX

```auto
buffer [5]
0:0x49
1:0x51
2:0x46
3:0x50
4:0x54

```

so im trying to convert this buffer into a float that should be `13.26`, but if i try to use

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

```

I get` "IQFPT"` , which is the decimal values rather than the hex in the buffer.  
like i siad im probably be a dumb arse and its blatantly obvious , but after the fortnight ive had im amazed i can even type.  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [30 June 2021 18:34 UTC](https://discourse.nodered.org/t/object-to-float-value/47849/2 "2021-06-30T18:34:28Z")

</div>

It looks like a string. Try. msg.payload = parseFloat(msg.payload,toString());

---

<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:** [30 June 2021 19:40 UTC](https://discourse.nodered.org/t/object-to-float-value/47849/3 "2021-06-30T19:40:53Z")

</div>

> [@vikingsraven](#):
>
> ```auto
> msg.payload = Buffer.from(msg.payload,"hex").toString()
> return msg;
> 
> ```

For a source buffer of `[73,81,70,80,84]` you dont specify HEX in the `from()` (thats for when the data you are providing is a HEX string.

Here are some options...

```auto
var buf = Buffer.from([73,81,70,80,84])

buf.readFloatBE(0) //--> 857189
buf.readFloatLE(0) //--> 13308863488
buf.toString() //'IQFPT'
buf.toString("hex") //--> "4951465054"

```

NOTE: a float is 4 bytes (32 bit) - your data has 5 bytes.

---

<div class="post-metadata">

**Author:** ![vikingsraven](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vikingsraven/32/49243_2.png) [@vikingsraven](https://discourse.nodered.org/u/vikingsraven)\
**Post date:** [1 July 2021 06:34 UTC](https://discourse.nodered.org/t/object-to-float-value/47849/4 "2021-07-01T06:34:47Z")

</div>

Thanks guys , turns out the values i was given are decimal ascii numbers not the hex values so my duino was sending " IQFPT".  
😳

---

<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:** [15 July 2021 06:35 UTC](https://discourse.nodered.org/t/object-to-float-value/47849/5 "2021-07-15T06:35:34Z")

</div>

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