Browse thread
[Caml-list] read a float(double) in a file in a binary file
-
polux moon
- Yaron M. Minsky
-
Thorsten Ohl
- Xavier Leroy
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Xavier Leroy <xavier.leroy@i...> |
| Subject: | Re: [Caml-list] read a float(double) in a file in a binary file |
> > 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