Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] reading/converting little endian
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] reading/converting little endian
> I wonder if there is a way to read or convert little endian numbers
> in ocaml. I am trying to write an interface to WAV audio files, and their
> headers contain little endian as well as some
> big endian words...

Your best bet is to read byte per byte and reassemble words yourself:

let input_int2_le ic =
  let b1 = input_byte ic in
  let b2 = input_byte ic in
  b1 lor (b2 lsl 8)

let input_int2_be ic =
  let b1 = input_byte ic in
  let b2 = input_byte ic in
  (b1 lsl 8) lor b2

and similarly for 4-byte integers.

- Xavier Leroy
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners