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 LAA15864; Mon, 5 Aug 2002 11:30:03 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 LAA16021 for ; Mon, 5 Aug 2002 11:30:02 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g759TvD15603; Mon, 5 Aug 2002 11:29:57 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id LAA15944; Mon, 5 Aug 2002 11:29:56 +0200 (MET DST) Date: Mon, 5 Aug 2002 11:29:56 +0200 From: Xavier Leroy To: Thorsten Ohl Cc: polux moon , caml-list@inria.fr Subject: Re: [Caml-list] read a float(double) in a file in a binary file Message-ID: <20020805112956.B7554@pauillac.inria.fr> References: <15690.48147.967042.16431@wptx47.physik.uni-wuerzburg.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <15690.48147.967042.16431@wptx47.physik.uni-wuerzburg.de>; from ohl@physik.uni-wuerzburg.de on Fri, Aug 02, 2002 at 07:06:27PM +0200 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > > How can I read a float (double) in a binary format (output of a > > programe in c ) from a file ? Here is a sample implementation, without any unsafe operation. You didn't say whether your numbers are in big or little endian representation, so the code handles both. Hope this helps, - Xavier Leroy let output_float_big_endian oc f = let n = ref (Int64.bits_of_float f) in for i = 0 to 7 do output_byte oc (Int64.to_int (Int64.shift_right_logical !n 56)); n := Int64.shift_left !n 8 done let output_float_little_endian oc f = let n = ref (Int64.bits_of_float f) in for i = 0 to 7 do output_byte oc (Int64.to_int !n); n := Int64.shift_right_logical !n 8 done let input_float_big_endian oc = let n = ref Int64.zero in for i = 0 to 7 do let b = input_byte oc in n := Int64.logor (Int64.shift_left !n 8) (Int64.of_int b) done; Int64.float_of_bits !n let input_float_little_endian oc = let n = ref Int64.zero in for i = 0 to 7 do let b = input_byte oc in n := Int64.logor !n (Int64.shift_left (Int64.of_int b) (i*8)) done; Int64.float_of_bits !n ------------------- 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