There are 2 bytes. (High byte and low) How to get a float number from them.
Closed due to the fact that the issue is too general for the participants of Kromster , iluxa1810 , αλεχολυτ , Grundy , kmv Jun 2 '17 at 15:02 .
Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .
- one@Kromster, well, everything seems to be in bytes - Andrew NOP
- one@Andrey I get a response from the controller via Modbus RTU protocol. And these 2 bytes form in me the value of some parameter (let's say pressure) - Victor Sayapov
- 3@VictorSayapov is great, now go read the documentation for the controller and find out what this 2 bytes are and how they form the value. - Kromster
- one@Kromster - what for documentation, if everything is in bytes (as indicated above) - Igor
- oneif it is modbus RTU, then you need to take the specification for the device and watch. For example, there it can be said that you need to multiply the specified number by a certain coefficient or divide. And maybe a more complex formula. - KoVadim
3 answers
Any 4 bytes form a valid (or special) float number, so you can add zeros from any side and in any combination.
But! .. numbers with a dot can have different lengths. Maybe you have a so-called. half float, written in 2 bytes. See the picture:
As mentioned above, in the comments, now go read the documentation for your controller and find out what it is for 2 bytes and how they form the value - half float it, or a fixed point integer, or a mantissa / exponent separately, or a fraction, or else . According to this, already see how they should be transferred to a standard float.
According to MSDN, the byte order depends on the architecture of the computer. You can find out the architecture using the BitConverter.IsLittleEndian static field BitConverter.IsLittleEndian
Read more comments section here: https://msdn.microsoft.com/ru-ru/library/system.bitconverter%28v=vs.110%29.aspx
- Guiding the clarifying question of the Architecture of which computer on which the author is trying to glue them together? - Kromster
- @Kromster, of course, on which it will launch - the architecture of that one will receive. - Andrei NOP
- 2It is not the answer to the question. - Kromster
- Let's TC will come and say whether or not - Andrew NOP
The structure of numbers is described on the Wiki .
Single exactly has the following structure:
SEF
S is the sign, E is the exponent, I is the integer part, F is the fractional part. Like for integers, the sign bit is the most significant one.
Field sizes in bits:
1-8-23
Accordingly, if you submit the data in this order, then you should succeed.
Correspondingly, you can also get an array of bytes from a number known to you and see how it all works.
- There after all 4 bytes are taken for conversion. And I have them 2. In what order to take them. That is the question. - Victor Sayapov
- @VictorSayapov; If you are talking about the high byte and the low byte, then you should be interested in S and F, and everything else with zeros. - iluxa1810
- 2@ iluxa1810 here it has two bytes. What pieces do they represent from three parts? which bit of sixteen is a sign? where is the exhibitor, where is the mantissa? Kindergarten of some kind. - Igor
- one@Igor, well, in this case, the problem is not solved without clarification. - iluxa1810
