Completely untested.
Note 'MBF'
convert a single precision Microsoft Binary Format number
into a 64-bit IEEE floating point number
byte order => m3 | m2 | m1 | exponent
m1 is most significant byte => sbbb|bbbb
m3 is the least significant byte
m = mantissa byte
s = sign bit
b = bit
Any MBF with a zero exponent is zero
MBF is bias 128.
MBF places the decimal point before the assumed bit.
)
mbf=: 3 : 0
assert. y e. a. NB. character
assert. 1=#$y NB. vector
assert. 0=4|#y NB. multiple of 4
i=. a.i._4]\y
e=. _128+{:"1 i
s=. (_128<e)*_1^128<:2{"1 i
s * (2^e) * 1 + (256^3)%~256#.128 0 0|"(1) 2 1 0{"1 i
)
----- Original Message -----
From: PackRat <[email protected]>
Date: Sunday, August 23, 2009 4:34
Subject: Re: [Jchat] [newbie]how to read binary formatted file
To: Chat forum <[email protected]>
> Ric Sherlock wrote:
> > > From: emptist
> > > In q I've learned to read a kind of binary
> > > formatted historical price data file, which contains 8 4
> byte int
> > > data for each record, using: ...
> >
> > Putting together the nicely broken down steps that Roger gave,
> a J
> > translation of the above line of q is:
> > _8]\ _2(3!:4) 1!:1 <'path/filename'
>
> How would you do this with 4-byte Metastock (Computrac) data,
> which is
> a floating point number in the old Microsoft Binary Format
> (MBF), which
> was used in the original IBM PC's BASICA, GWBASIC, and
> QuickBASIC and
> is different from the now-standard IEEE floating point format
> (which J
> uses)?
>
> I recently purchased some historical market data which turned
> out to be
> in Metastock format. Fortunately, there are about a half
> dozen open
> source software packages that will do the full conversion from
> Metastock files to comma-delimited ASCII files. However,
> for a while I
> had entertained the thought of seeing if this were possible in
> J.
> Since my J knowledge isn't all that great, I quickly gave up the
> idea,
> though! But I'd still be interested in working bit by bit (pun?)
> on
> such a personal project if I knew how to do the 4-byte binary
> format
> conversion. (I think the rest of the binary data is
> reasonably
> straightforward to parse--but then again...) Here are
> salient excerpts
> from info found in a couple of the source code projects:
>
>
> SINGLE PRECISION FLOATING POINT:
>
> /* MS Binary
> Format */
> /* byte order => m3 | m2 | m1 | exponent */
> /* m1 is most significant byte => sbbb|bbbb */
> /* m3 is the least significant
> byte */
> /* m = mantissa
> byte */
> /* s = sign
> bit */
> /* b =
> bit */
> /* */
> /* IEEE Single Precision Float
> Format */
> /*
> m3 m2 m1 exponent */
> /* mmmm|mmmm mmmm|mmmm emmm|mmmm seee|eeee */
> /* s =
> sign bit */
> /* e =
> exponent bit */
> /* m =
> mantissa bit */
> /* */
> /* any msbin w/ exponent of zero = zero */
> /* */
> /* MBF is bias 128 and IEEE is bias 127. ALSO, MBF
> places */
> /* the decimal point before the assumed bit,
> while */
> /* IEEE places the decimal point after the assumed
> bit. */
>
>
> DOUBLE PRECISION FLOATING POINT:
>
> /* MS Binary
> Format */
> /* byte order => m7 | m6 | m5 | m4 | m3 | m2 | m1 |
> exponent */
> /* m1 is most significant byte =>
> smmm|mmmm */
> /* m7 is the least significant
> byte */
> /* m = mantissa
> byte */
> /* s = sign
> bit */
> /* b =
> bit */
> /* */
> /* IEEE Double Precision Float
> Format */
> /* byte 8 byte 7 byte
> 6 byte 5 byte 4 and so
> on */
> /* seee|eeee eeee|mmmm mmmm|mmmm mmmm|mmmm mmmm|mmmm
> ... */
> /* s =
> sign bit */
> /* e =
> exponent bit */
> /* m =
> mantissa bit */
> /* */
> /* any msbin w/ exponent of zero =
> zero */
> /* */
> /* MBF is bias 128 and IEEE is bias 1023. ALSO, MBF
> places */
> /* the decimal point before the assumed bit,
> while */
> /* IEEE places the decimal point after the assumed
> bit. */
> /* */
> /* IEEE has a half byte less for its mantissa. If the
> msbin */
> /* number has anything in this last half byte, then there
> is */
> /* an
> overflow. */
>
>
> I included the double precision info for the sake of
> completeness, but
> it's not part of the Metastock format, so the double precision
> conversion is not necessary (though it may be an interesting
> challenge--
> you never know what kind of data is lurking out there!).
>
> Thanks in advance for any conversion code you or someone else
> can
> provide!
>
> Harvey
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm