Convert IEEE-754 Single Precision Float to Javascript Float

External devices (particularly Modbus) often make values available as a 32 bit IEEE-754 value [1]. It may be available as a 4 byte buffer or array, a hex string or a 32 bit integer. This flow will convert any of those into their equivalent floating point value. When given an array or buffer it assumes the bytes are provided most significant byte first. If that is not the case then it will be necessary to reorder them first (or adjust the code to allow for that).

[1] https://en.wikipedia.org/wiki/Single-precision_floating-point_format

[{"id":"f64c029.2c5838","type":"function","z":"2ae8b25a.bffaa6","name":"IEEE-754 to float","func":"/* Converts from an number, string, buffer or array representing an IEEE-754 value\n * to a javascript float.\n * The following may be given in msg.payload:\n *      A string representing a number, which may be hex or binary\n *        examples, \"1735\"  \"0x02045789\"  0b01000000010010010000111111011011\n *      An integer value\n *      A four element array or buffer of 8 bit values, most significant byte first.\n*/ \n// first make a number from the given payload if necessary\nlet intValue;\nif (typeof msg.payload === \"number\") {\n    intValue = msg.payload;\n} else if (typeof msg.payload === \"string\") {\n    intValue = Number(msg.payload);\n} else if (msg.payload.length == 4) {\n    // four byte array or buffer\n    intValue = (((((msg.payload[0] << 8) + msg.payload[1]) << 8) + msg.payload[2]) << 8) + msg.payload[3];\n} else {\n    node.warn(\"Unrecognised payload type or length\");\n}\n\nmsg.payload = Int2Float32(intValue);\nreturn msg;\n\nfunction Int2Float32(bytes) {\n    var sign = (bytes & 0x80000000) ? -1 : 1;\n    var exponent = ((bytes >> 23) & 0xFF) - 127;\n    var significand = (bytes & ~(-1 << 23));\n\n    if (exponent == 128) \n        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);\n\n    if (exponent == -127) {\n        if (significand === 0) return sign * 0.0;\n        exponent = -126;\n        significand /= (1 << 22);\n    } else significand = (significand | (1 << 23)) / (1 << 23);\n\n    return sign * significand * Math.pow(2, exponent);\n}","outputs":1,"noerr":0,"x":385,"y":724,"wires":[["6e888b6a.40c874"]]},{"id":"6e888b6a.40c874","type":"debug","z":"2ae8b25a.bffaa6","name":"Value","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":574,"y":724,"wires":[]},{"id":"2dc3b0d4.06517","type":"inject","z":"2ae8b25a.bffaa6","name":"1 string hex","topic":"","payload":"0x3f800000","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":105,"y":614,"wires":[["f64c029.2c5838"]]},{"id":"43e54f8e.0cf598","type":"inject","z":"2ae8b25a.bffaa6","name":"-2 string hex","topic":"","payload":"0xc0000000","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":104,"y":650,"wires":[["f64c029.2c5838"]]},{"id":"a8fc7865.b2a438","type":"inject","z":"2ae8b25a.bffaa6","name":"Pi string hex","topic":"","payload":"0x40490fdb","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":104,"y":684,"wires":[["f64c029.2c5838"]]},{"id":"f2c4c56.6390138","type":"inject","z":"2ae8b25a.bffaa6","name":"Pi string dec","topic":"","payload":"1078530011","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":101.5,"y":758,"wires":[["f64c029.2c5838"]]},{"id":"e830bb54.c9cf08","type":"inject","z":"2ae8b25a.bffaa6","name":"Pi string binary","topic":"","payload":"0b01000000010010010000111111011011","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":112,"y":720,"wires":[["f64c029.2c5838"]]},{"id":"d1f3247b.ea8798","type":"inject","z":"2ae8b25a.bffaa6","name":"Pi integer","topic":"","payload":"1078530011","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":97,"y":545,"wires":[["f64c029.2c5838"]]},{"id":"d7bfbaa2.85c4c8","type":"inject","z":"2ae8b25a.bffaa6","name":"Pi Array","topic":"","payload":"[64,73,15,219]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":83,"y":817,"wires":[["f64c029.2c5838"]]},{"id":"70a1c40f.9a925c","type":"inject","z":"2ae8b25a.bffaa6","name":"Pi Buffer","topic":"","payload":"[64,73,15,219]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":92,"y":855,"wires":[["f64c029.2c5838"]]}]

Flow Info

Created 6 years, 6 months ago
Rating: 3 1

Owner

Actions

Rate:

Node Types

Core
  • debug (x1)
  • function (x1)
  • inject (x8)

Tags

  • IEEE-754
  • modbus
  • float
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option