[Solved] Hex to 32-bit float

how to convert hex (in string) to 32-bit float (IEEE 754)?

You’ll have to use a Function Node to achieve this. The Function Node exposes the Buffer Object, which is very good an doing conversions like this.

So for example, if I have a hex string on the payload that represents the float 3.14159 at data.hex, I can convert it back to the float using the following function:

var hex = payload.data.hex;
var b = Buffer.from(hex, 'hex');
var value = b.readFloatLE(0);
payload.data.value = value; // 3.14159

You can now access the float value in the following nodes on the data.value payload path.

Hi Brandon.

Had no idea the Buffer object was exposed. I can’t find any documentation in Losant relating to this.

Can you outline what other node api/object/entities are also exposed in the function node or point to documentation ?

Also is the Buffer object exposed on the Edge as well as Application workflows ?

Thanks

Tim

For cloud workflows, Buffer is the only additional object available. For edge workflows, since they run on your own hardware, you can use require and import any core NodeJS module you’d like.

If you want to get fancy, you can even extend the base agent Docker container and globally install any npm module. If it’s installed globally, you can then use require in the Function Node to use it.

The Function Node doesn’t support async execution, so that does limit available modules to things that run synchronously, but still opens a lot of options on the edge.

The documentation could use more details. Thanks for pointing that out!