From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id EAA14297; Wed, 4 Jul 2001 04:21:54 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id EAA14300 for ; Wed, 4 Jul 2001 04:21:52 +0200 (MET DST) Received: from c012.sfo.cp.net (c012-h020.c012.sfo.cp.net [209.228.12.168]) by concorde.inria.fr (8.11.1/8.10.0) with SMTP id f642Loj10560 for ; Wed, 4 Jul 2001 04:21:51 +0200 (MET DST) Received: (cpmta 688 invoked from network); 3 Jul 2001 19:21:48 -0700 Date: 3 Jul 2001 19:21:48 -0700 Message-ID: <20010704022148.687.cpmta@c012.sfo.cp.net> X-Sent: 4 Jul 2001 02:21:48 GMT Received: from [24.152.1.84] by mail.altavista.com with HTTP; 03 Jul 2001 19:21:48 PDT Content-Type: text/plain Content-Disposition: inline Mime-Version: 1.0 To: jarvarvar@airtel.net From: Arturo Borquez Cc: caml-list@inria.fr X-Mailer: Web Mail 3.9.3.3 Subject: Re: [Caml-list] Using 16 bits integers X-Sent-From: aborquez@altavista.com Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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