caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Arturo Borquez <aborquez@altavista.com>
To: jarvarvar@airtel.net
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Using 16 bits integers
Date: 3 Jul 2001 19:21:48 -0700	[thread overview]
Message-ID: <20010704022148.687.cpmta@c012.sfo.cp.net> (raw)

On Tue, 03 July 2001, jarvarvar@airtel.net wrote:

> 
> Hello, I'm just new to OCaml. I'm writing a program wich uses a lot of
> 16 bits operations, and writes them to a buffer. For writing into the
> buffer, it is needed for the data to be in 16 bits, not in 32 bits.
> Exactly, the program is like a sound synthesizer, so data cannot be send
> to sound card in 32 bits.
> My problem with OCaml is: how can I manage 16 bits data, and make
> operations with it? I had not seen any type in Ocaml with 16bits lenght
> (int is 32 bits in my machine). This will frustate my project if I do
> not find a solution, so I will use C if there is no other solution...
> but I would not like to do if I can avoid it, because I like very much
> Ocaml.
> 
> I'm hopping your answers. Thanks.
> 
Hi,
Very simple. Do all your programm computations in
OCaml native ints (32 or 64 bit CPU) and when put
these ints into the buffer then 'convert' them to
16 bits.

Example:

let put_int16_into_buffer buffer position int_value =
  buffer.[pos] <- char_of_int (int_value land 0xff);
  buffer.[pos + 1] <- char_of_int (int_value lsr 8)

You must verify endianness of your CPU to fetch LH or
HL. The example shows the case of little-endian used
in all Intel CPU's. In a similar way you can retrieve
ints from a buffer

let get_int16_to_int_from_buffer buffer position =
  let int_value = (int_of_char buffer.[position] +
    ((int_of_char buffer.[position + 1]) * 256) in
  int_value

Best Regards
Arturo Borquez 





Find the best deals on the web at AltaVista Shopping!
http://www.shopping.altavista.com
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


             reply	other threads:[~2001-07-04  2:21 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-04  2:21 Arturo Borquez [this message]
  -- strict thread matches above, loose matches on Subject: below --
2001-07-03 18:05 jarvarvar
2001-07-03 18:21 ` Brian Rogoff
2001-07-03 18:34 ` David McClain

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=20010704022148.687.cpmta@c012.sfo.cp.net \
    --to=aborquez@altavista.com \
    --cc=caml-list@inria.fr \
    --cc=jarvarvar@airtel.net \
    /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;
as well as URLs for NNTP newsgroup(s).