From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 4F15BBBFA for ; Fri, 17 Jun 2005 20:10:43 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j5HIAgsN028446 for ; Fri, 17 Jun 2005 20:10:42 +0200 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 UAA01463 for ; Fri, 17 Jun 2005 20:10:42 +0200 (MET DST) Received: from smtp3.wanadoo.fr (smtp3.wanadoo.fr [193.252.22.28]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j5HIAfIE028442 for ; Fri, 17 Jun 2005 20:10:42 +0200 Received: from me-wanadoo.net (localhost [127.0.0.1]) by mwinf0301.wanadoo.fr (SMTP Server) with ESMTP id C67471C00571 for ; Fri, 17 Jun 2005 20:10:41 +0200 (CEST) Received: from morgana (ARennes-257-1-25-200.w81-53.abo.wanadoo.fr [81.53.8.200]) by mwinf0301.wanadoo.fr (SMTP Server) with ESMTP id 810E81C00551; Fri, 17 Jun 2005 20:10:41 +0200 (CEST) X-ME-UUID: 20050617181041528.810E81C00551@mwinf0301.wanadoo.fr Received: from david by morgana with local (Exim 4.50) id 1DjLIe-0001Mk-IS; Fri, 17 Jun 2005 20:10:40 +0200 To: "Nicolas Cannasse" Cc: Subject: Re: [Caml-list] How to handle endianness and binary string conversion for 32 bits integers (Int32)? References: <87slzima67.fsf@linux-france.org> <004501c572c2$5305ad30$19b0e152@warp> From: David MENTRE Organization: none Date: Fri, 17 Jun 2005 20:10:40 +0200 In-Reply-To: <004501c572c2$5305ad30$19b0e152@warp> (Nicolas Cannasse's message of "Fri, 17 Jun 2005 00:25:38 +0200") Message-ID: <8764wcc0rj.fsf@linux-france.org> User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Miltered: at concorde with ID 42B31222.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 42B31221.002 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 binary:01 integers:01 cannasse:01 warplayer:01 ocaml:01 converts:01 endian:01 binary:01 byte:01 char:01 char:01 byte:01 endian:01 converts:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: Hello, "Nicolas Cannasse" 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"): <>= open Int32 @ \section{32 bits integer to/from string conversion} Function [[be_of_int32]] converts an [[Int32]] into its big endian binary representation. <>= 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]]. <>= 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} <>= 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 5996 CC46 4612 9CA4 3562 D7AC 6C67 9E96 A3AD 7A2A