Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Pierre Weis <Pierre.Weis@inria.fr>
To: whitley@cse.buffalo.edu (John Whitley)
Cc: caml-list@inria.fr
Subject: Re: Binary file I/O
Date: Fri, 20 Nov 1998 09:36:15 +0100 (MET)	[thread overview]
Message-ID: <199811200836.JAA18006@pauillac.inria.fr> (raw)
In-Reply-To: <13907.9032.867059.29471@pollux.cs.Buffalo.EDU> from "John Whitley" at Nov 20, 98 00:07:07 am

> 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/





      reply	other threads:[~1998-11-20  8:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-11-20  5:07 John Whitley
1998-11-20  8:36 ` Pierre Weis [this message]

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=199811200836.JAA18006@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=whitley@cse.buffalo.edu \
    /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