Version française
Home     About     Download     Resources     Contact us    
Browse thread
How to handle endianness and binary string conversion for 32 bits integers (Int32)?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nicolas George <nicolas.george@e...>
Subject: Re: [Caml-list] How to handle endianness and binary string conversion for 32 bits integers (Int32)?
L'octidi 28 prairial, an CCXIII, David MENTRE a écrit :
> Except that I'm writing a network interface that should be specified so
> that externally written programs can read my data.

My point is: you do not need to convert between big and little endian, you
only have to convert between sequences of bytes and integers. You never need
even to know that integers are stored as bytes in the memory of the
computer.

In fact, I expect that you could not find access to the internal
representation of integers in OCaml, which is a good thing. Most of the
portability bugs in C come from people who have not understood the principle
of having access to the internal representation of objects (which is not to
use it :-). Alas, a lot of books about C are very i86-centered and explain
it really badly.

> 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.

I think this is really exactly the same approach, and I am flattered to have
the same position af Kernighan.