# IEEE-754 Floating Point Converter

**URL:** <https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833>\
**Category:** General\
**Created:** [13 November 2018 10:51 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833 "2018-11-13T10:51:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aabbas](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aabbas](https://discourse.nodered.org/u/aabbas)\
**Post date:** [13 November 2018 10:51 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/1 "2018-11-13T10:51:42Z")

</div>

Hello, I am using Node-RED to convert convert IEEE-754 Floating Point to decimal.  
I am using this code to convert the values wich I get the right positive values but I obtain -Infinity for the negative values.

Code:  
var buf = Buffer.from(msg.payload);  
var sign = buf.readFloatBE() \>\> 31 === 0 ? 1 : -1 ;  
var exponent = (buf.readFloatBE() & 0b01111111100000000000000000000000);  
var significand = (buf.readFloatBE() & ~(-1 \<\< 23));  
msg.payload = sign \* significand \* Math.pow(2, exponent);  
return msg;

Can you please help me to get the negative values.

---

<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:** [13 November 2018 11:09 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/2 "2018-11-13T11:09:40Z")

</div>

> [@aabbas](#):
>
> var exponent = (buf.readFloatBE() & 0b01111111100000000000000000000000)

That exponent calc doesn't look right, it will give you a huge number if the exponent is non-zero. I would have expected to see it shifted right.  
Are you sure you get the right values for +ve numbers? Have you tried with a big +ve?  
Where did you get the code?

---

<div class="post-metadata">

**Author:** ![aabbas](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aabbas](https://discourse.nodered.org/u/aabbas)\
**Post date:** [13 November 2018 11:32 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/3 "2018-11-13T11:32:35Z")

</div>

Yes, I get the right +ve values. I didn't tried with a big +ve values ( The maximum value that I converted it is about 4000).

---

<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:** [13 November 2018 12:22 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/4 "2018-11-13T12:22:25Z")

</div>

Where did you get that code? It is not right.

---

<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:** [13 November 2018 15:06 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/5 "2018-11-13T15:06:54Z")

</div>

Or if you did not copy some code where is the algorithm you are trying to replicate?

---

<div class="post-metadata">

**Author:** ![aabbas](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aabbas](https://discourse.nodered.org/u/aabbas)\
**Post date:** [13 November 2018 15:33 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/6 "2018-11-13T15:33:07Z")

</div>

> [@Colin](#):
>
> code where is the algorithm you are trying to replicate?

I have tried several code after I changed them to get the value that I had . Can you help me please ?

---

<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:** [13 November 2018 15:49 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/7 "2018-11-13T15:49:11Z")

</div>

I am not going to start working out the algorithm to do the conversion. If you google and find the algorithm to use (which presumably you have already in order to get nearly there) then I can help you code it in javascript.

---

<div class="post-metadata">

**Author:** ![aabbas](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aabbas](https://discourse.nodered.org/u/aabbas)\
**Post date:** [13 November 2018 16:19 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/8 "2018-11-13T16:19:37Z")

</div>

```auto

I found this code but I didn't get the right Values

function Bytes2Float32(bytes) {
    var sign = (bytes & 0x80000000) ? -1 : 1;
    var exponent = ((bytes >> 23) & 0xFF) - 127;
    var significand = (bytes & ~(-1 << 23));

    if (exponent == 128) 
        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);

    if (exponent == -127) {
        if (significand == 0) return sign * 0.0;
        exponent = -126;
        significand /= (1 << 22);
    } else significand = (significand | (1 << 23)) / (1 << 23);

    return sign * significand * Math.pow(2, exponent);
}

```

---

<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:** [13 November 2018 17:27 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/9 "2018-11-13T17:27:58Z")

</div>

That looks more likely to be correct. Have you got some sample values and what they should convert to?

---

<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:** [13 November 2018 17:45 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/10 "2018-11-13T17:45:20Z")

</div>

That function works for me with examples from [https://en.wikipedia.org/wiki/Single-precision\_floating-point\_format](https://en.wikipedia.org/wiki/Single-precision_floating-point_format)

```auto
[{"id":"f64c029.2c5838","type":"function","z":"2ae8b25a.bffaa6","name":"Bytes to float","func":"let bytes = Number(msg.payload);\nmsg.payload = Bytes2Float32(bytes)\nreturn msg;\n\nfunction Bytes2Float32(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":310,"y":612,"wires":[["6e888b6a.40c874"]]},{"id":"6e888b6a.40c874","type":"debug","z":"2ae8b25a.bffaa6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":502,"y":612,"wires":[]},{"id":"2dc3b0d4.06517","type":"inject","z":"2ae8b25a.bffaa6","name":"1","topic":"","payload":"0x3f800000","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":89,"y":614,"wires":[["f64c029.2c5838"]]},{"id":"43e54f8e.0cf598","type":"inject","z":"2ae8b25a.bffaa6","name":"-2","topic":"","payload":"0xc0000000","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":84,"y":650,"wires":[["f64c029.2c5838"]]},{"id":"a8fc7865.b2a438","type":"inject","z":"2ae8b25a.bffaa6","name":"Pi","topic":"","payload":"0x40490fdb","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":82,"y":684,"wires":[["f64c029.2c5838"]]}]

```

---

<div class="post-metadata">

**Author:** ![aabbas](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aabbas](https://discourse.nodered.org/u/aabbas)\
**Post date:** [13 November 2018 18:49 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/11 "2018-11-13T18:49:01Z")

</div>

Thank you very much @Colin.  
But I have just one question to ask. befor the function node I have a node which I obtain a buffer like this [67,76,0,0]. I have to change Number to buffer? like this :  
let bytes = buffer.from(msg.payload);  
msg.payload = Bytes2Float32(bytes)  
return msg;  
I changed it but I get always 0.

---

<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:** [13 November 2018 20:32 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/12 "2018-11-13T20:32:08Z")

</div>

It is important to be careful in your choice of words. You say you have a buffer like [67,67,0,0] but then say you want to use buffer.from on it. If it is already a buffer then why do buffer.from? I suspect you mean that it is an array. If so then there is no need to convert it to a buffer. If the array is in msg.payload you can do

```auto
let bytes = (((((msg.payload[0] << 8) + msg.payload[1]) << 8) + msg.payload[2]) << 8) + msg.payload[3]
msg.payload = Bytes2Float32(bytes)
return msg;

```

---

<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:** [14 November 2018 11:00 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/13 "2018-11-14T11:00:24Z")

</div>

The wikipedia post has an error. Not in the conversion but 3.1415927410 is not pi!

[Edit] No it isn't, that is as close as you can get in single precision float.

---

<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:** [14 November 2018 21:06 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/14 "2018-11-14T21:06:24Z")

</div>

As this has come up before I have added a flow to flows library with the function to do this conversion.

[https://flows.nodered.org/flow/359ead34237b7ab6ec0465ee85a34b62](https://flows.nodered.org/flow/359ead34237b7ab6ec0465ee85a34b62)

---

<div class="post-metadata">

**Author:** ![aabbas](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aabbas](https://discourse.nodered.org/u/aabbas)\
**Post date:** [15 November 2018 08:44 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/15 "2018-11-15T08:44:15Z")

</div>

Thank you very much @Colin.

---

<div class="post-metadata">

**Author:** ![skiv](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/skiv/32/10074_2.png) [@skiv](https://discourse.nodered.org/u/skiv)\
**Post date:** [11 July 2019 09:01 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/16 "2019-07-11T09:01:19Z")

</div>

Need help. How to modify this function to convert from double (binary64) to decimal.  
I try this, but it's not work

```auto

let intValue;
if (typeof msg.payload === "number") {
    intValue = msg.payload;
} else if (typeof msg.payload === "string") {
    intValue = Number(msg.payload);
} else if (msg.payload.length == 4) {
    // four byte array or buffer
    intValue = (((((msg.payload[0] << 16) + msg.payload[1]) << 16) + msg.payload[2]) << 16) + msg.payload[3];
} else {
    node.warn("Unrecognised payload type or length");
}

msg.payload = Int2Float32(intValue);
return msg;

function Int2Float32(bytes) {
    var sign = (bytes & 0x80000000) ? -1 : 1;
    var exponent = ((bytes >> 52) & 0xFF) - 1023;
    var significand = (bytes & ~(-1 << 52));

    if (exponent == 1) 
        return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);

    if (exponent == -1023) {
        if (significand === 0) return sign * 0.0;
        exponent = -1022;
        significand /= (1 << 51);
    } else significand = (significand | (1 << 52)) / (1 << 52);

    return sign * significand * Math.pow(2, exponent);
}

```

---

<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:** [11 July 2019 09:18 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/17 "2019-07-11T09:18:56Z")

</div>

Send the incoming message to a debug node and show us what you see, otherwise we don't know what you are starting with

---

<div class="post-metadata">

**Author:** ![skiv](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/skiv/32/10074_2.png) [@skiv](https://discourse.nodered.org/u/skiv)\
**Post date:** [11 July 2019 09:49 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/18 "2019-07-11T09:49:29Z")

</div>

```auto
msg.payload : Object
object
data: array[4]
0: 16550
1: 25493
2: 13159
3: 782
buffer: buffer[8][raw](http://127.0.0.1:1880/#)
0: 0x40
1: 0xa6
2: 0x63
3: 0x95
4: 0x33
5: 0x67
6: 0x3
7: 0xe

```

It's mast be 2.87 kWh

Device which i read registers is pac3200, offset 801, 4 regesters, double format, Overflow 1.0e+12 .

After converting, I have  
msg.payload : number  
2.8824866727954215e-292

This converter ([https://www.binaryconvert.com/result\_double.html?hexadecimal=40A663953367300E](https://www.binaryconvert.com/result_double.html?hexadecimal=40A663953367300E)) tell me, decimal value = 2.86579140779935460159322246909E3

---

<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:** [11 July 2019 12:40 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/19 "2019-07-11T12:40:35Z")

</div>

Please post a flow including an inject node and your function showing it not working.  
Note that you have the wrong link (it should end 030E not 300E) but that does not make much difference.

---

<div class="post-metadata">

**Author:** ![skiv](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/skiv/32/10074_2.png) [@skiv](https://discourse.nodered.org/u/skiv)\
**Post date:** [11 July 2019 13:53 UTC](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833/20 "2019-07-11T13:53:10Z")

</div>

`[{"id":"f2043821.10b2a8","type":"function","z":"7b2adc08.02a074","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] << 16) + msg.payload[1]) << 16) + msg.payload[2]) << 16) + 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 >> 52) & 0xFF) - 1023;\n var significand = (bytes & ~(-1 << 52));\n\n if (exponent == 1) \n return sign * ((significand) ? Number.NaN : Number.POSITIVE_INFINITY);\n\n if (exponent == -1023) {\n if (significand === 0) return sign * 0.0;\n exponent = -1022;\n significand /= (1 << 51);\n } else significand = (significand | (1 << 52)) / (1 << 52);\n\n return sign * significand * Math.pow(2, exponent);\n}","outputs":1,"noerr":0,"x":390,"y":80,"wires":[["583f7267.d123bc"]]},{"id":"583f7267.d123bc","type":"debug","z":"7b2adc08.02a074","name":"Value","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":600,"y":80,"wires":[]},{"id":"485407f3.a2f768","type":"inject","z":"7b2adc08.02a074","name":"fromeRegesters","topic":"","payload":"[16550,25493,13159,782]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":80,"wires":[["f2043821.10b2a8"]]}]`

Thanks. You are right about 030E.

[Next page](https://discourse.nodered.org/t/ieee-754-floating-point-converter/4833.md?page=2)
