From: Luca Pascali <pasckosky2000@yahoo.it>
To: 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 09:29:20 +0200 [thread overview]
Message-ID: <42B27BD0.8090005@yahoo.it> (raw)
In-Reply-To: <87aclqm725.fsf@linux-france.org>
David MENTRE wrote:
>Hello Nicolas,
>
>Nicolas George <nicolas.george@ens.fr> 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
next prev parent reply other threads:[~2005-06-17 7:29 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 [this message]
2005-06-16 22:25 ` Nicolas Cannasse
2005-06-17 18:10 ` David MENTRE
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=42B27BD0.8090005@yahoo.it \
--to=pasckosky2000@yahoo.it \
--cc=caml-list@inria.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