Has anyone ever come across Microsoft Binary Format (MBF) and knows how to convert it to float that can be used? This is for a project with an Arduino Nano Every using the ModbusRTUMaster library. The slave device has a 32bit float in MBF stored in 2 consecutive 16-bit registers, it's a temperature reading from a sensor. I can read the registers, but I don't know how to convert MBF to IEEE-754 32bit float.
Thank you both for the fast replies. It's certainly not IEEE-754. Here are some details from the manufacturer about the stored value:
float = 32-bit single precision floating point value stored in two subsequent 16-bit registers. Register n, [EEEE EEEE SMMM MMMM] – exponent (offset 129), sign and mantisa Register n+1, [MMMM MMMM MMMM MMMM] – mantisa examples:
+100.25 is coded as 8748h, 8000h
-12.5 is coded as 84C8h, 0000h
+3.1415 is coded as 8249h, 0E56h
The exponent would need to be converted to the proper signed number, after which the sign bit and exponent would need to be moved. Looks like the maitissa could stay where it is.
Looks like the bias is 129 for MBF, and 127 for IEEE-754, a difference of 2.
Need to test for the special case of an exponent of 00h representing 0.0, and be careful of going out of range.
< edit >
Does appear to work by shifting the sign 8 bits to the left, subtracting 2 from the exponent and placing it in the proper location, with the mantissa unchanged. Have not looked at how to code the special cases of an exponent of 00h for zero, and FFh that IEEE-754 uses for NaN and infinity.
Also need to be careful of byte order, all the Arduino boards I am familiar with are little-endian.
The MBF and IEEE-754 floating point formats both store the exponent as an unsigned 8-bit number, while the actual exponent is a signed number. To convert from the signed to unsigned number, MBF adds 129, while IEEE-754 adds 127. Converting from MBF to IEEE-754 requires that you subtract 129, to get the signed exponent value, then add 127 for the IEEE-754 representation, which is a net effect of subtracting 2.
There is going to be a slight difference in the range of numbers that each format can represent, since an MBF exponent of 1 cannot be converted to IEEE-754, and an MBF exponent of FFh would be FDh in IEEE-754, making it impossible to have a conversion that produced FEh. Note that 00h has special meanings in both formats, and FFh has special uses in IEEE-754.
You'll want to confirm that zero is encoded in MBF as all-zero. As for the bias of the exponent, if you find the temperature readings are off by exactly some power of two, you can tweak the magic 0x81 in the MicrosoftBinaryFormat::exponent function.
It can as a subnormal number. You may get round-off errors, though.
Then again, not all IEEE-754 subnormal numbers can be converted back to MBF, since MBF doesn't support subnormal numbers. Most subnormal numbers will simply underflow to zero when converted to MBF.