# \[Solved\] IEEE 754 Floating point conversion

**URL:** <https://forums.losant.com/t/solved-ieee-754-floating-point-conversion/1477>\
**Category:** Help\
**Tags:** edge, modbus\
**Created:** [January 25, 2019, 6:15pm UTC](https://forums.losant.com/t/solved-ieee-754-floating-point-conversion/1477 "2019-01-25T18:15:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Weaver](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@James\_Weaver](https://forums.losant.com/u/James_Weaver)\
**Post date:** [January 25, 2019, 6:15pm UTC](https://forums.losant.com/t/solved-ieee-754-floating-point-conversion/1477/1 "2019-01-25T18:15:01Z")

</div>

I have set up an Edge device to read Modbus. I am successfully connected and sending information using 16 Bit Modicon. I am sending a single register with the value of 1. The hex value for the number 1 in 16 bit Modicon is 0000 3F80 The problem is that this data is begin converted into a integer in the Losant platform it is converting each hexadecimal block individually so 3F80 in a programmer calculator is 16256 it is an improper conversion this should be converted using a floating point converter or left in a raw state so a script could be used for the conversion.

Is there any plan to bring the hex data back in raw format? It would also be nice if Losant supported several Modbus protocols such as 16 bit Modicon, 16 Bit Word Swapped so there would be less scripting involved. These protocols are widely used in the industrial world.

Here is a screen shot of the packet log in my slave device.  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/1X/59ad4b7ee6c8e10af83d68b32e0d40feaff1e457.png)

No options to bring raw hex data back to Losant.  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/1X/cb6425484af595e3fd2c50648c145637ee008f06.png)

Thank you  
Jimmy

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [January 25, 2019, 10:05pm UTC](https://forums.losant.com/t/solved-ieee-754-floating-point-conversion/1477/2 "2019-01-25T22:05:45Z")

</div>

We’ve got some plans to allow other conversion types, however you can work around this issue using a [Function Node](https://docs.losant.com/workflows/logic/function/), which provides access to the [Buffer Object](https://nodejs.org/api/buffer.html) to perform all sorts of conversions.

Even though the result is cast as an integer, it’s still the same bytes underneath. We can load up a Buffer with those bytes and then read them out in different forms. The function would look something like this:

```auto
var buf = new Buffer(4);
buf.writeUInt32BE(payload.data.myRegister); // unsigned int 16256
payload.data.myRegisterFloat = buf.readFloatBE() // 2.2779507836064226e-41

```

The Buffer object provides a lot of power to convert bytes to different types (including Big/Little endianness). Definitely check it out and see if it solves your issue.

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [January 25, 2019, 10:06pm UTC](https://forums.losant.com/t/solved-ieee-754-floating-point-conversion/1477/3 "2019-01-25T22:06:34Z")

</div>

You can also get the raw hex back by running:

```auto
buf.toString('hex'); // 00003f80

```

---

<div class="post-metadata">

**Author:** ![F\_C](https://avatars.discourse-cdn.com/v4/letter/f/3bc359/32.png) [@F\_C](https://forums.losant.com/u/F_C)\
**Post date:** [August 8, 2019, 8:09am UTC](https://forums.losant.com/t/solved-ieee-754-floating-point-conversion/1477/4 "2019-08-08T08:09:39Z")

</div>

Hi,  
After spending a bit of time working out how to turn two holding register int16’s back into a float32 I thought it would be helpful to post for others.

As above but with a few tweeks:  
var buf = new Buffer(4);  
buf.writeUInt16BE(payload.results.modbus1.HoldingRegistersInt[6],0); // signed int 15846  
buf.writeUInt16BE(payload.results.modbus1.HoldingRegistersInt[5],2); // signed int 26240  
payload.results.modbus1.A1 = buf.readFloatBE() // float32 0.11250019073486328

Notes:

- Make sure the ints are signed in the Modbus read, the buf.writeUInt16BE doesn’t seem to like unsigned.
- Make sure the Endianness is the same eg. Big (in Modbus read) -BE

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [August 8, 2019, 5:38pm UTC](https://forums.losant.com/t/solved-ieee-754-floating-point-conversion/1477/5 "2019-08-08T17:38:43Z")

</div>

Wow! Thanks for your input @F_C 🙂

---

<div class="post-metadata">

**Author:** ![James\_Weaver](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@James\_Weaver](https://forums.losant.com/u/James_Weaver)\
**Post date:** [September 19, 2019, 12:16am UTC](https://forums.losant.com/t/solved-ieee-754-floating-point-conversion/1477/6 "2019-09-19T00:16:53Z")

</div>

F\_C

This is exactly the solution to the issue. I had given up on trying to solve the issue as I am no programmer. Thank you for the effort and sharing.

Jimmy
