From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 932B3BCAF for ; Fri, 17 Jun 2005 09:29:23 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j5H7TNE4030298 for ; Fri, 17 Jun 2005 09:29:23 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id JAA25603 for ; Fri, 17 Jun 2005 09:29:22 +0200 (MET DST) Received: from smtp001.mail.ukl.yahoo.com (smtp001.mail.ukl.yahoo.com [217.12.11.32]) by nez-perce.inria.fr (8.13.0/8.13.0) with SMTP id j5H7TMmj030295 for ; Fri, 17 Jun 2005 09:29:22 +0200 Received: (qmail 98896 invoked from network); 17 Jun 2005 07:29:22 -0000 Received: from unknown (HELO ?10.0.0.115?) (pasckosky@213.255.109.130 with plain) by smtp001.mail.ukl.yahoo.com with SMTP; 17 Jun 2005 07:29:21 -0000 Message-ID: <42B27BD0.8090005@yahoo.it> Date: Fri, 17 Jun 2005 09:29:20 +0200 From: Luca Pascali User-Agent: Debian Thunderbird 1.0.2 (X11/20050331) X-Accept-Language: en-us, en MIME-Version: 1.0 To: caml-list@inria.fr Subject: Re: [Caml-list] How to handle endianness and binary string conversion for 32 bits integers (Int32)? References: <87slzima67.fsf@linux-france.org> <20050616190206.GA553@clipper.ens.fr> <87aclqm725.fsf@linux-france.org> In-Reply-To: <87aclqm725.fsf@linux-france.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 42B27BD3.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 42B27BD2.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 binary:01 integers:01 endian:01 integers:01 externally:01 byte:01 byte:01 ocaml:01 ocaml:01 sprintf:01 char:01 baretta:01 baretta:01 covert:98 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.5 required=5.0 tests=FROM_ENDS_IN_NUMS autolearn=disabled version=3.0.2 X-Spam-Level: David MENTRE wrote: >Hello Nicolas, > >Nicolas George writes: > > > >>> 1. convert between big and little endian 32 bits integers; >>> >>> >>Don't do that. >> >> > >Except that I'm writing a network interface that should be specified so >that externally written programs can read my data. > >In fact, your post reminded me of a chapter of "The Practice of >Programming" by Kernighan and Pike. They advise to read/write integers >byte by byte using 8 bit masking and shifting. > >So I'll follow that approach, which is close to yours and is independent >of machine endianness. > >Thank you for the tip, ;-) >Yours, >d. > > C language provides, for this purpose, some functions: htons htonl ntohs ntohl (and I don't remember if there are for other formats) hton_ functions covert numbers from machine endianess (whatever it is) to network endianess, ntoh_ is the contrary. These functions are always present, if needed they swap bytes into the integer, otherwise they are just identity. I can suggest to use them instead of doing the same job in Ocaml. An alternative for doing the job in Ocaml is to convert the number in exadecimal through a sprintf "%08x" and covert to a char each couple (rember to use the *0* in the format to have a 8 chars string as result). Then, you can send the first couple (MSB) first to produce an endianess, or send the last couple (LSB) to have the other one. On the other side, you can get characters and build the number using mathematical formula. In this way you are not aware of your local endianess, but you are sure about the endianess of the link. It is longer, but works. LP -- ********************************************************************* Luca Pascali luca@barettadeit.com asxcaml-guru@barettadeit.com http://www.barettadeit.com/ Baretta DE&IT A division of Baretta SRL tel. 02 370 111 55 fax. 02 370 111 54 Our technology: http://www.asxcaml.org/ http://www.freerp.org/ ___________________________________ Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB http://mail.yahoo.it