![]() |
Forum Index : Microcontroller and PC projects : Converting IEEE754
Author | Message | ||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Hi All, Can Anyone who's not as vague as me on Binary Maths tell me if I'm on the right track trying to convert this data. My Modbus Energy Meters return a HEX value that need to be converted to a float. It's documented here on page 14 of their manual. Can't quite get my head around the math, but I'm attempting to treat the data as an integer then move the decimal place in binary form by dividing by 65,536 IE 2^16. Not sure if I'm on the right track or not even though this code gives back 240.5. I'm at this stage ignoring the sign & not sure how the exponent relates to 2^16=65536. Thanks Phil Returns this.... [Code] DOS MMBasic Ver 5.04.08 Copyright 2011-2017 Geoff Graham 43708000 01000011011100001000000000000000 10000110 134 11111111111100001000000000000000 011100001000000000000000 111100001000000000000000 111100001000000000000000 15761408 240.5 > [/code] |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Try this with a few other numbers It simply pokes the 4 bytes into a variable of type float and lets MMBasic do the conversion. I am not sure about all the different IEEE standards but it give the right answer for the supplied bytes. Jim VK7JH MMedit |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Thanks Jim, Works with &h43708000=240.5 & &hc3708000= -240.5, but happened to try &hc82e7570 & it returned -178646 not -178645.75. Seems to Truncate the decimal in quite a few examples. But then realised that my code below, which is now working fine in the DOS version, does a similar thing when I load it into an E64 that has Version 5.0302. Output on the E64. [Code] print mm.ver 5.0302 > run Hex Value: 487a7293 &h487a7293 Hex Value Reading: 487A7293 Binary Value: 01001000011110100111001010010011 Sign Bit: 0 Exponent & -offset: 10010000 17 Full Mantissa: 111110100111001010010011 Decimal Value: 16413331 Binary Shift Applied: 256458 Sign Bit Applied: 256458 [/code] Output from DOS MMbasic. [Code] > print mm.ver 5.0408 > run Hex Value: 487a7293 &h487a7293 Hex Value Reading: 487A7293 Binary Value: 01001000011110100111001010010011 Sign Bit: 0 Exponent & -offset: 10010000 17 Full Mantissa: 111110100111001010010011 Decimal Value: 16413331 Binary Shift Applied: 256458.2969 Sign Bit Applied: 256458.2969 [/code] Edit:- original number encoded was 256458.3 & did it here... https://www.h-schmidt.net/FloatConverter/IEEE754.html |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Floating point numbers get PRINTED with a reduced number of significant figures to give a true representartion of their precision. Change my original program to PRINT with more digits: You might end up with problems if/when you use a 'mite with double precision floats. Your code might be safe. Jim VK7JH MMedit |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Thanks Jim, Think I tried STR$(a,6,2) at some stage this morning, but then went on a tangent & loaded 5.04.09 Beta 16 & thought that was the answer. Phil. |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Yep, Just re-flashed the Beta to try & Poking the variable makes it return a zero value. Phil. |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Think My Binary Math understanding has improved in the past few days. Final solution seems to work fine. No glaring Foo-pars or incorrect practices? Phil. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
You have ended up with a nice useful function. It will be handy as double precision becomes more prevalent. Jim VK7JH MMedit |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
Hello Phil. Hope you are well. I would like to see your very useful IEEE to MMBasic FP conversion Function in the new MMBasic library at http://www.fruitoftheshed.com/ this ensures it is preserved and is find-able for anyone in a similar situation. Are you ok with this? You can create the wiki page yourself to document the Function or I can if you prefer (you'll get the credit and there will be a pointer to this discussion) Let me know how you are with this. Thanks Andrew |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |