caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David MENTRE <dmentre@linux-france.org>
To: "Nicolas Cannasse" <warplayer@free.fr>
Cc: <caml-list@inria.fr>
Subject: Re: [Caml-list] How to handle endianness and binary string conversion for 32 bits integers (Int32)?
Date: Fri, 17 Jun 2005 20:10:40 +0200	[thread overview]
Message-ID: <8764wcc0rj.fsf@linux-france.org> (raw)
In-Reply-To: <004501c572c2$5305ad30$19b0e152@warp> (Nicolas Cannasse's message of "Fri, 17 Jun 2005 00:25:38 +0200")

Hello,

"Nicolas Cannasse" <warplayer@free.fr> writes:

> Extlib IO module have some code about that,
> See http://ocaml-lib.sourceforge.net/doc/IO.html 

I've look at this code but, from a quick look, it seems to handle only
OCaml int.

For what is worth, here is my own weel (here under public domain
"license"):


<<timestamp.ml>>=
open Int32
@ 

\section{32 bits integer to/from string conversion}

Function [[be_of_int32]] converts an [[Int32]] into its big
endian binary representation.

<<timestamp.ml>>=
let be_of_int32 n =
  let byte_mask = of_int 0xff in
  let char_of_int32 x = Char.chr (to_int x) in
  let d0 = char_of_int32 (logand n byte_mask) in
  let d1 = char_of_int32 (logand (shift_right_logical n 8) byte_mask) in
  let d2 = char_of_int32 (logand (shift_right_logical n 16) byte_mask) in
  let d3 = char_of_int32 (logand (shift_right_logical n 24) byte_mask) in
  let big_endian = String.make 4 d3 in
  big_endian.[1] <- d2;
  big_endian.[2] <- d1;
  big_endian.[3] <- d0;
  big_endian
@ 


Function [[int32_of_be]] converts an big endian binary
representation of a 32 bits integer into an [[Int32]].

<<timestamp.ml>>=
let int32_of_be be =
  if String.length be <> 4 then 
   raise (Invalid_argument "int32_from_big_endian");
  let d3 = of_int (Char.code be.[3])
  and d2 = of_int (Char.code be.[2]) 
  and d1 = of_int (Char.code be.[1]) 
  and d0 = of_int (Char.code be.[0]) in
  (logor (shift_left d0 24) 
     (logor (shift_left d1 16) 
        (logor (shift_left d2 8) d3)))
@ 



\section{Automatic tests}

<<timestamp.ml>>=
let _ =
  if Config.do_autotests then begin
    Printf.printf "  timestamp autotests...";
    assert(int32_of_be "\001\002\003\004" = of_string "0x01020304");
    assert(be_of_int32 (of_string "0x01020304") = "\001\002\003\004");
    assert(int32_of_be "\255\254\253\252" = of_string "0xfffefdfc");
    assert(be_of_int32 (of_string "0xfffefdfc") = "\255\254\253\252");
    Printf.printf "done\n"
  end
@ 


Yours,
d.
-- 
pub  1024D/A3AD7A2A 2004-10-03 David MENTRE <dmentre@linux-france.org>
 5996 CC46 4612 9CA4 3562  D7AC 6C67 9E96 A3AD 7A2A


      reply	other threads:[~2005-06-17 18:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-16 18:25 David MENTRE
2005-06-16 19:02 ` [Caml-list] " Nicolas George
2005-06-16 19:32   ` David MENTRE
2005-06-16 20:14     ` Nicolas George
2005-06-17  7:29     ` Luca Pascali
2005-06-16 22:25 ` Nicolas Cannasse
2005-06-17 18:10   ` David MENTRE [this message]

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=8764wcc0rj.fsf@linux-france.org \
    --to=dmentre@linux-france.org \
    --cc=caml-list@inria.fr \
    --cc=warplayer@free.fr \
    /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).