Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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 Archives:
Bug reports: FAQ:
Beginner's list: