caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Thorsten Ohl <ohl@physik.uni-wuerzburg.de>
To: "polux moon" <polux_moon@hotmail.com>
Cc: caml-list@inria.fr
Subject: [Caml-list] read a float(double)  in a file in a binary file
Date: Fri, 2 Aug 2002 19:06:27 +0200	[thread overview]
Message-ID: <15690.48147.967042.16431@wptx47.physik.uni-wuerzburg.de> (raw)
In-Reply-To: <F186SweAcWKHlQAiPgT0000ad6a@hotmail.com>

polux moon <polux_moon@hotmail.com> writes:

> How can I read a float (double) in a binary format (output of a
> programe in c ) from a file ?

Here's what I use:

  (* This is hard coded value for Linux/Intel: *)
    let little_endian = true

    external float_of_bytes : string -> float = "float_of_bytes"

    let unsafe_rev8 s =
      let swap i1 i2 =
        let tmp = String.unsafe_get s i1 in
        String.unsafe_set s i1 (String.unsafe_get s i2);
        String.unsafe_set s i2 tmp in
      swap 0 7;
      swap 1 6;
      swap 2 5;
      swap 3 4

    let input_binary_float ic =
      let buf = String.create 8 in
      really_input ic buf 0 8;
      if little_endian then
        unsafe_rev8 buf;
      float_of_bytes buf

    let input_binary_floats ic array =
      let n = Array.length array in
      let bytes = 8 * n in
      let buf = String.create bytes in
      really_input ic buf 0 bytes;
      for i = 0 to n - 1 do
        let s = String.sub buf (8 * i) 8 in
        if little_endian then
          unsafe_rev8 s;
        array.(i) <- float_of_bytes s
      done

-- 
Thorsten Ohl, Physics Dept., Wuerzburg Univ. -- ohl@physik.uni-wuerzburg.de
http://theorie.physik.uni-wuerzburg.de/~ohl/     [<=== PGP public key here]
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2002-08-03 21:26 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-01 11:05 polux moon
2002-08-02 16:58 ` Yaron M. Minsky
2002-08-02 17:06 ` Thorsten Ohl [this message]
2002-08-05  9:29   ` Xavier Leroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15690.48147.967042.16431@wptx47.physik.uni-wuerzburg.de \
    --to=ohl@physik.uni-wuerzburg.de \
    --cc=caml-list@inria.fr \
    --cc=polux_moon@hotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).