Convert from Micosoft Binary Format to numeric and back.

412 views
Skip to first unread message

geoffre...@gmail.com

unread,
Jul 24, 2024, 12:36:05 AM7/24/24
to Harbour Users
I need to convert from Microsoft Binary Format (MBF) to numeric and back.

MBF was developed by Microsoft for their early GW-BASIC & Visual Basic etc programs. It converted from numeric to binary format suitable for storage on disk, and occupies 32 bits.

MBF was superceded by the IEEE 754 standard which was released some years later. MBF uses 4 bytes for storage while IEEE uses 8 bytes and occupies 64 bits of memory.

My application is to convert data from Metastock charting programs to numeric. Metastock (which is the default format for most charting programs) uses the MBF format.

I did come across some MBF conversion functions in Clipper many years ago, but one had a bug in it. Those functions were called CVI(), CVS(), MKI() and MKS(0 etc.

Anyone know if Harbour has these functions, and secondly are there any C library functions around which will convert from numeric to MBF?


AL67

unread,
Jul 25, 2024, 3:49:23 AM7/25/24
to Harbour Users

Geoffrey Kenan

unread,
Jul 25, 2024, 4:06:33 AM7/25/24
to 'AL67' via Harbour Users
No.

These routines convert from MBF to/from IEEE 754 format only. If I used them them I would also need another routine - which I do not have - to convert from IEEE to numeric etc.

There are old routines out there somewhere. Also some commercial products using an API but I do not want to do that. 

Back to my original question - anyone know if Harbour has these functions, and secondly are there any C library functions around which will convert from numeric to MBF?
--
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: https://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/0c9a5f71-f576-41ca-ae7f-55442f878452n%40googlegroups.com.


AL67

unread,
Jul 25, 2024, 6:15:37 AM7/25/24
to Harbour Users
Functions on this page use float type numbers for input and output,
so simply  cast input 4 bytes (from Harbour) to float  number (check bytes order !) , call function,
and return to Harbour by :  hb_retnd(  (double) float_output_number)

Geoffrey Kenan

unread,
Jul 25, 2024, 7:36:46 PM7/25/24
to harbou...@googlegroups.com
No, hb_retnd is not what I need

AL67

unread,
Jul 26, 2024, 2:45:54 AM7/26/24
to Harbour Users
"I need to convert from Microsoft Binary Format (MBF) to numeric and back"

In C (and also in Harbour) floating point values ​​are encoded in the IEEE 754 standard.
Define what You mean by  NUMERIC ?

Geoffrey Kenan

unread,
Jul 26, 2024, 4:52:01 AM7/26/24
to 'AL67' via Harbour Users
Numeric is a simple number.  Some call it Float, but they are not in a group I am in - I am not a C programmer.

What else can it mean?

Geoffrey Kenan

unread,
Jul 26, 2024, 7:00:04 PM7/26/24
to harbou...@googlegroups.com
Numeric (format) is another way of saying number.  You use the term float. Same thing, except float can mean something else to me which is why I avoid using it.

AL67

unread,
Jul 29, 2024, 4:24:52 AM7/29/24
to Harbour Users
Try this code (only Harbour :) )

PROCEDURE mian

SET DECIMAL TO  10
 ? mbf2num( CHR(0xDB)+CHR(0x0F)+CHR(0x49)+CHR(0x83) )    // 2* pi      by  www  LE byte order
 ? mbf2num( CHR(0xF3)+CHR(0x04)+CHR(0x35)+CHR(0x81) )    // sqrt(2)

 ? hb_strtohex(Num2mbf(2 * 3.14159265358979324))
 ? hb_strtohex(Num2mbf(3.14159265358979324 / 2))
 ? hb_strtohex(Num2mbf( log(2) ))
 ? hb_strtohex(Num2mbf( -log(2) ))
 ? hb_strtohex(Num2mbf( sqrt(2) ))

RETURN


//********************************************************

FUNCTION MBF2Num( cBytes )   // MBF LE bytes order   m2 m1 s+m0 exp
LOCAL m:=0,e,minus:=1               // mantissa , exponent, sign
LOCAL b1:=ASC(SUBSTR(cBytes,1,1))   // m2
LOCAL b2:=ASC(SUBSTR(cBytes,2,1))   // m1
LOCAL b3:=ASC(SUBSTR(cBytes,3,1))   // signBit + m0(7bits)
LOCAL b4:=ASC(SUBSTR(cBytes,4,1))   // exponent


IF b4==0         // zero
   RETURN 0
ENDIF

IF b3 > 127      // minus value
  minus := -1
  b3 -= 128      // reset sign bit
ENDIF

// e := b4 - 128
e := b4 - 129   // -128 - 1  by info in comment on page https://www.experts-exchange.com/questions/20245266/Need-to-access-MBF-Microsoft-Binary-Format-data.html

m := (m + b1) / 256
m := (m + b2) / 256
m := (m + b3) / 128

RETURN minus * 2^e * (1+m)

//************************
FUNCTION Num2MBF( nNum )
LOCAL b1,b2,b3,b4
LOCAL m,e,minus:=0, x

IF nNum == 0
  RETURN CHR(0)+CHR(0)+CHR(0)+CHR(0)
ENDIF

IF nNum < 0
  minus:=128
  nNum := -nNum
ENDIF

e := LOG(nNum)/LOG(2)    //exponent
IF e < 0
  e := INT(e) - 1
ELSE
  e := INT(e)
ENDIF

IF e < -128 .OR. e > 126
   RETURN  NIL     // RETURN ""   overflow
END

b4 := CHR(e + 129)  // 128+1 by info on www

m := ( nNum / (2^e) ) - 1    //mantissa - 1

x := INT( m * 128 )
m := (m * 128) - x
b3 := CHR(x + minus)        //sign+m0(7bits)

x := INT( m * 256 )
m := (m * 256) - x
b2 := CHR(x)               //m1

x := INT( m * 256 )
m := (m * 256) - x
IF m>=0.5  .AND. x<255   //when x==255 then ++b2 also maybe ++b3 also overflow
  ++x
ENDIF
b1 := CHR(x)              //m2

RETURN  b1+b2+b3+b4      //m2 m1 s+m0  exp


Geoffrey Kenan

unread,
Jul 29, 2024, 7:13:25 PM7/29/24
to harbou...@googlegroups.com
Yes, this code works. I have checked it going both ways.

Thank you for the effort in doing this. Appreciated.

But it is slow when converting a large number of records from Metastock - as there often can be in a Metastock file. 

If anyone comes across some C code I would appreciate that as well.

AL67

unread,
Jul 30, 2024, 7:17:49 AM7/30/24
to Harbour Users
Try.
_mbf_c.prg

AL67

unread,
Aug 6, 2024, 1:25:02 AM8/6/24
to Harbour Users
Geoffrey, does the code with C functions run much faster?

Adam
Reply all
Reply to author
Forward
0 new messages