Knowledge Base - Microsoft Binary Format to IEEE Double Precision Floating Point Conversion
Context - A client has an application written in the late 1980s, in what appears to be QuickBasic
3.0. The data is written in binary flat files, so a fixed length record of, for example, 300 bytes contains,
say, 30 columns, some of which are the floating point data type supported at the time. Microsoft used
a software defined floating point in various versions of GWBasic and QuickBasic (as well as other programming
languages, including C++) that predates the definition of the IEEE floating point standard used with the Intel
math co-processors. QuickBasic 4 and subsequent versions use the IEEE standard.
A keyword search of 'csharp mbf to ieee floating point conversion' produces a number of results, some of
which involve invoking functions in a C++ COM library, others of which are questions asking for help and don't
contain code. Many times the reponses to the inquries link to Microsoft MSDN or Knowledge Base links.
The code presented below is an implementation of the MBF format conversion in C#. It has been designed
to be easily debugged during single-step tracing, and is not, therefore, particularly 'efficient' from
an execution standpoint. The project that this is written for is part of a one-time conversion,
so efficiency takes a back seat to auditability.
Since a C# program doesn't have an MBF data type, this is likely to arrive inside the program as a byte stream
read from a file. As such, it will be in 'little endian' format, with leading zeros, the mantissa
appearing to be right justified, followed in the last byte by the exponent. Anyone using this code
would have to study up on the MBF and IEEE formats, respectively, which are extensively documented by Microsoft
and Wikipedia, among others.
The specific purpose of publishing this code is to get third party code review. The use of this code
is at the consumers own risk, no warranty is express or implied. Since the published email address is
being spammed, the way to submit comments is through
LinkedIn. Search for 'Resource Logic Meredith
Poor'. There is a 'Resource Logic' in California and one in Texas, this is the Texas one. Send
an invite with contact information and a comment, and someone here will respond if appropriate.