Loading ...
Sorry, an error occurred while loading the content.
 

Reading/writing binary data

Expand Messages
  • Mark White
    I d like to write some OCaml routines to read and write large arrays from an (already defined) binary file format, in several numeric types (big/little endian,
    Message 1 of 5 , Nov 30, 2005
      I'd like to write some OCaml routines to read and write
      large arrays from an (already defined) binary file format,
      in several numeric types (big/little endian, short/long
      ints, single/double prec floats). Is there a convenient way
      to do this without resorting to writing a C module?

      I've found the IO module in ocaml-lib, which is quite
      helpful; biggest issue it doesn't seem to read/write
      single-precision IEEE floats. I'd be perfectly happy to
      use doubles internally and convert them at the IO stage.

      Apologies if I've missed an obvious answer somewhere.

      Mark <><
    • Richard Jones
      ... This looks like an omission from extlib which ought to be fixed. I ve just checked and it doesn t exist in CVS either. (X-posted to the extlib development
      Message 2 of 5 , Nov 30, 2005
        On Wed, Nov 30, 2005 at 01:45:03PM +0000, Mark White wrote:
        > I'd like to write some OCaml routines to read and write
        > large arrays from an (already defined) binary file format,
        > in several numeric types (big/little endian, short/long
        > ints, single/double prec floats). Is there a convenient way
        > to do this without resorting to writing a C module?
        >
        > I've found the IO module in ocaml-lib, which is quite
        > helpful; biggest issue it doesn't seem to read/write
        > single-precision IEEE floats. I'd be perfectly happy to
        > use doubles internally and convert them at the IO stage.

        This looks like an omission from extlib which ought to be fixed. I've
        just checked and it doesn't exist in CVS either.

        (X-posted to the extlib development list)

        Does someone who understands single-precision floating point formats
        want to comment on whether this is possible in pure OCaml?

        Rich.

        --
        Richard Jones, CTO Merjis Ltd.
        Merjis - web marketing and technology - http://merjis.com
        Team Notepad - intranets and extranets for business - http://team-notepad.com
      • Mark White
        ... Ok, sure -- a read_single there would make sense. ... I ve just noticed a work-around using the Int32 module: Int32.float_of_bits (IO.read_real_i32 ip)
        Message 3 of 5 , Nov 30, 2005
          Richard Jones writes:
          > On Wed, Nov 30, 2005 at 01:45:03PM +0000, Mark White wrote:
          > > I've found the IO module in ocaml-lib, which is quite
          > > helpful; biggest issue it doesn't seem to read/write
          > > single-precision IEEE floats. I'd be perfectly happy to
          > > use doubles internally and convert them at the IO stage.
          >
          > This looks like an omission from extlib which ought to be fixed. I've
          > just checked and it doesn't exist in CVS either.
          >
          > (X-posted to the extlib development list)

          Ok, sure -- a read_single there would make sense.

          > Does someone who understands single-precision floating point formats
          > want to comment on whether this is possible in pure OCaml?

          I've just noticed a work-around using the Int32 module:

          Int32.float_of_bits (IO.read_real_i32 ip)

          which seems to do the right thing.

          Thanks,
          Mark <><
        • Zhifeng Sheng
          Dear friends I am trying to count the memory my program is using. I use Gc module and do like this let stat = Gc.quick_stat in printf %e (stat.minor_words +.
          Message 4 of 5 , Dec 1, 2005
            Dear friends

            I am trying to count the memory my program is using. I use Gc module
            and do like this

            let stat = Gc.quick_stat in
            printf "%e" (stat.minor_words +. stat.major_words -.
            stat.promoted_words)

            but it won't work....
            the error message is

            Unbound record field label minor_words

            Can someone help me with it ?

            Thank you
            Best wishes
            Zhifeng Sheng
          • Jon Harrop
            ... Yes. This is simply a namespace problem. If you have a value stat of the type Gc.stat and you want to access its field called minor_words then you
            Message 5 of 5 , Dec 1, 2005
              On Thursday 01 December 2005 14:13, Zhifeng Sheng wrote:
              > I am trying to count the memory my program is using. I use Gc module
              > and do like this
              >
              > let stat = Gc.quick_stat in
              > printf "%e" (stat.minor_words +. stat.major_words -.
              > stat.promoted_words)
              >
              > but it won't work....
              > the error message is
              >
              > Unbound record field label minor_words
              >
              > Can someone help me with it ?

              Yes. This is simply a namespace problem. If you have a value "stat" of the
              type "Gc.stat" and you want to access its field called "minor_words" then you
              must enter the namespace of the module "Gc" between the value name and the
              field name, like this:

              stat.Gc.minor_words

              This has nothing to do with the task at hand, it is simply the way OCaml
              handles the naming of records (stat) and modules (Gc).

              --
              Dr Jon D Harrop, Flying Frog Consultancy Ltd.
              Objective CAML for Scientists
              http://www.ffconsultancy.com/products/ocaml_for_scientists
            Your message has been successfully submitted and would be delivered to recipients shortly.