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

Re: "ocaml_beginners"::[] Writing binary values to a file

Expand Messages
  • Andrei Formiga
    Yes, I have thought about that. What worries me is that the encoding program will depend on OCaml s internal representation for ints. But I guess there s no
    Message 1 of 7 , Oct 31, 2003
    • 0 Attachment
      Yes, I have thought about that. What worries me is
      that the encoding program will depend on OCaml's
      internal representation for ints. But I guess there's
      no other way. Thanks for the answers.

      ---
      []s, Andrei de A. Formiga


      --- Pleban <phm@...> wrote:
      >
      > use I/O routines for reading/writing single bytes:
      > input_byte,
      > output_byte. then you can write 16-bits (2 x 1 byte)
      > and 32-bits (4 x 1
      > byte) values without any problems.
      >
      > consider following example:
      >
      > let write32bit x out =
      > for i = 0 to 3 do
      > (output_byte out (Int32.to_int
      > (Int32.logand
      > (Int32.shift_right_logical
      > (Int32.of_int x) (8*i))
      > (Int32.of_int 255))));
      > done;;
      >
      > write32bit 0x31323334 stdout;;
      >
      >
      > .s.s.
      > sszymczy@...
      >
      >


      __________________________________
      Do you Yahoo!?
      Exclusive Video Premiere - Britney Spears
      http://launch.yahoo.com/promos/britneyspears/
    • Martin Jambon
      ... It would be nice to have an Int32.output_binary function, and some others. Maybe you should post some suggestions to caml-list@inria.fr Martin
      Message 2 of 7 , Nov 3, 2003
      • 0 Attachment
        On Fri, 31 Oct 2003, Andrei Formiga wrote:

        >
        > Yes, I have thought about that. What worries me is
        > that the encoding program will depend on OCaml's
        > internal representation for ints. But I guess there's
        > no other way. Thanks for the answers.

        It would be nice to have an Int32.output_binary function,
        and some others. Maybe you should post some suggestions to
        caml-list@...

        Martin
      • Remi Vanicat
        ... One solution is to map the file using the function Bigarray.Array1.map_file This function create a one dimension bigarray whose content is a file. To
        Message 3 of 7 , Nov 3, 2003
        • 0 Attachment
          Andrei Formiga <ktulu_fhtagn@...> writes:

          > Hello everyone,
          >
          > I want to encode an image to a specific binary
          > format (BMP), writing the results in a file. The image
          > has image data plus headers.
          >
          > The first problem is that I can't use OCaml's int
          > type for that, as it's encoded differently. Or at
          > least I can't simply write the int value, not even
          > using output_binary_int, if I understand the manual
          > correctly. Also, the header has fields of 32 and 16
          > bits, and the image data will mostly consist of 8-bit
          > bytes. For the bytes it's possible to use output_byte.
          > What I don't know is how to write the 16-bits and
          > 32-bits values correctly without depending too much on
          > OCaml's internal representation for ints. For some
          > 32-bits values I can use nativeint, but how can I
          > output a nativeint in binary format ?
          >
          > Overall, my question is: how can I do that (write a
          > binary file in a predetermined format) in OCaml ?

          One solution is to map the file using the function
          Bigarray.Array1.map_file

          This function create a one dimension bigarray whose content is a
          file. To modify the bigarray modify the file, So it might be a
          solution.
          --
          RĂ©mi Vanicat
        • Stefano Zacchiroli
          ... And also to the ocaml extlib mailing list ... it s easier that such features will be implemented there. Cheers. -- Stefano Zacchiroli -- Master in
          Message 4 of 7 , Nov 4, 2003
          • 0 Attachment
            On Mon, Nov 03, 2003 at 01:02:06PM +0100, Martin Jambon wrote:
            > It would be nice to have an Int32.output_binary function,
            > and some others. Maybe you should post some suggestions to
            > caml-list@...

            And also to the ocaml extlib mailing list ... it's easier that such
            features will be implemented there.

            Cheers.

            --
            Stefano Zacchiroli -- Master in Computer Science @ Uni. Bologna, Italy
            zack@{cs.unibo.it,debian.org,bononia.it} - http://www.bononia.it/zack/
            " I know you believe you understood what you think I said, but I am not
            sure you realize that what you heard is not what I meant! " -- G.Romney
          Your message has been successfully submitted and would be delivered to recipients shortly.