From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA20767; Sat, 3 Aug 2002 23:26:32 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA21468 for caml-list@pauillac.inria.fr; Sat, 3 Aug 2002 23:26:32 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id TAA04446 for ; Fri, 2 Aug 2002 19:06:29 +0200 (MET DST) Received: from wptx49.physik.uni-wuerzburg.de (wptx49.physik.uni-wuerzburg.de [132.187.40.49]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g72H6T516537 for ; Fri, 2 Aug 2002 19:06:29 +0200 (MET DST) Received: from wptx47.physik.uni-wuerzburg.de (wptx47.physik.uni-wuerzburg.de [132.187.40.47]) by wptx49.physik.uni-wuerzburg.de (8.10.0/8.10.0.Beta6) with ESMTP id g72H6Sj27378; Fri, 2 Aug 2002 19:06:28 +0200 (METDST) Received: (from ohl@localhost) by wptx47.physik.uni-wuerzburg.de (8.10.0.Beta12/8.10.0.Beta12) id g72H6RA01373; Fri, 2 Aug 2002 19:06:27 +0200 From: Thorsten Ohl Message-ID: <15690.48147.967042.16431@wptx47.physik.uni-wuerzburg.de> Date: Fri, 2 Aug 2002 19:06:27 +0200 To: "polux moon" Cc: caml-list@inria.fr Subject: [Caml-list] read a float(double) in a file in a binary file In-Reply-To: References: X-Mailer: VM 6.84 under Emacs 20.7.1 Reply-To: ohl@physik.uni-wuerzburg.de Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk polux moon 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