I've just written perl program to access metastock files. Metastock data files doesn't use IEEE Floating Point Format. They use MS Floating. I've implemented function to convert MSBIN -> IEEE (Only 4 bytes float). OK These is a code.
open(FP,"f20.dat"); # Open MetaStock DATA File :> read (FP, $buf, 0x1c); # Skip header for 7 fields $i = 0; while (!eof(FP)) { read(FP, $buf, 0x1c); # Fetch 7 fields data ($date, $open, $high, $low, $close, $volume, $opt) = unpack("A4 A4 A4 A4 A4 A4 A4", $buf); ## Although they are float but I fetch them as strings. $date = mem2date($date); $open = mem2date($open); $high = mem2date($high); $low = mem2date($low); $close = mem2date($close); $volume = mem2date($volume); print "$date $open $high $low $close $volume\n"; ++$i; last if ($i > 10); ### I want see only first 10 records. } close(FP); ####################################### sub mem2date { my $mem = $_[0]; my ($a, $b, $c, $d) = unpack ("C C C C", $mem); # /* 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 */ # sign = msbin[2] & 0x80; /* 1000|0000b */ # /* 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 */ my ($msexp, $man, $sign, $iexp, $newc, $newd); $msexp = $d; $man = $c & 0x7f; $sign = $c & 0x80; $iexp = $msexp - 2; ## This line is very necessary because MS is bias 128 but IEEE is bias + 127 $newd = ($iexp >> 1) | $sign; $newc = ($iexp << 7) | $man; $c = $newc; $d = $newd; return unpack("f",pack("CCCC",$a, $b, $c, $d)); } --- End of Sample Code ---
If you want more information about IEEE Floating Point format please visit

http://www.psc.edu/general/software/packages/ieee/ieee.html

But MS Floating Point format is hard to find. I read from

http://community.borland.com/article/0,1410,16431,00.html

Sorry for my wrong grammar and poor english.


In reply to Converting Floating Point MS to IEEE by bream

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.