-1

enter image description hereI am reading values from a S7-300 PLC with my c# code. When the values are in INT format there is no problem. But there are some 32 bit memory areas (Double Words) that are encoded in IEEE 754 Floating-Point standart. (First bit is sign bit, the next 8 bits exponent, and the remaining 23 bits Mantissa) I can read out this memory areas from the PLC only as Int32 (As they were integer).

How can I convert this as integer read value to a single Real value in C# with respeect of the IEEE 754 Floating-Point encoding in the double word?

Mdarende
  • 469
  • 3
  • 13
  • 2
    If you can read the value as integer then you can use an approach like this : `var finalSingle= BitConverter.ToSingle(BitConverter.GetBytes(s7Int))` – Eldar Sep 17 '22 at 14:44
  • Please people, post an answer! It's frustrating to open a question, read it and then read the comments and find that the issue has been resolved. If you're going to post code that answers the question then post it as an answer, not a comment. If someone resolves your issue without posting an answer then post an answer yourself. Don't leave resolved questions unanswered. – user18387401 Sep 17 '22 at 15:44
  • 1
    Does this answer your question? [Convert int bits to float bits](https://stackoverflow.com/questions/27237776/convert-int-bits-to-float-bits) – phuclv Sep 17 '22 at 16:09
  • @user18387401 no there are already lots of duplicates [How do I Mimic Number.intBitsToFloat() in C#?](https://stackoverflow.com/q/43993601/995714), [Convert int bits to float bits](https://stackoverflow.com/q/27237776/995714), [C# floating point to binary string and vice versa](https://stackoverflow.com/q/59704806/995714). It's also easy to find by reading the documentation – phuclv Sep 17 '22 at 16:11

1 Answers1

1

It worked just as wanted with Eldar's answer. If you read a 32 bit float value as bit, then just convert it like this: Thanks again to Eldar :-)

var finalSingle= BitConverter.ToSingle(BitConverter.GetBytes(s7Int))

Mdarende
  • 469
  • 3
  • 13