caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Binary file I/O
@ 1998-11-20  5:07 John Whitley
  1998-11-20  8:36 ` Pierre Weis
  0 siblings, 1 reply; 2+ messages in thread
From: John Whitley @ 1998-11-20  5:07 UTC (permalink / raw)
  To: caml-list


I am about to implement a wavelet-based audio compression algorithm,
part of my Ph.D. research, in OCaml.  To this end, I must read and
write binary data streams representing the input digital audio and the
output compressed audio stream.

Is there a standard way of handling binary file I/O in OCaml, or must
I resort to handling input and output bitstream formatting in C?

Thanks,
John Whitley




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Binary file I/O
  1998-11-20  5:07 Binary file I/O John Whitley
@ 1998-11-20  8:36 ` Pierre Weis
  0 siblings, 0 replies; 2+ messages in thread
From: Pierre Weis @ 1998-11-20  8:36 UTC (permalink / raw)
  To: John Whitley; +Cc: caml-list

> Is there a standard way of handling binary file I/O in OCaml, or must
> I resort to handling input and output bitstream formatting in C?
> 
> Thanks,
> John Whitley

The standard way to input and output binary files in Caml is to use
the bin versions of the channel opening primitives open_in and
open_out, namely open_in_bin and open_out_bin (those primitives
prevent the interpretation and convertion of end-of-line
characters). Then you read/write the channel as usual, either one
character at a time using input_char/output_char or directly via a
string buffer of your own using the input/output primitives. Direct
handling of binary integers is also available using
input_binary_int/output_binary_int.

In any case, you may access to the bits of your bitstream by first
accessing the characters and then applying the usual mask and shift
stuff (lnot) (land) (lor) (lsr) (lsl).

For easier handling of these operations you may define associated
infix symbols, for instance, trying to be (loosely) reminiscent of C
syntax:

let ( << ) = (lsl)
and ( >> ) = (lsr)
and ( &! ) = (land)
and ( |! ) = (lor)
and ( ~! ) = (lnot);;

Then proceed as usual, for instance

let nth_bit n j = (n &! (0x1 << j)) >> j;;

let nth_bit_char c j = nth_bit (Char.code c) j;;

Best regards,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1998-11-20  8:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-11-20  5:07 Binary file I/O John Whitley
1998-11-20  8:36 ` Pierre Weis

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).