Date: Sat, 10 Oct 1998 05:37:59 +0100 (BST) From: George Foot To: djgpp AT delorie DOT com Subject: Re: newbie: endianess In-Reply-To: <361EB530.DD492547@cartsys.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Reply-To: djgpp AT delorie DOT com On Fri, 9 Oct 1998, Nate Eldredge wrote: > Sigurdur Smarason wrote: > > > > I'm trying to find a way to set the endianess for file i/o but I > > don't seem to be able to find any documentation on it. Does anybody know > > where I should go to find more info on the matter ? > > You can't change the endianness, a file is just a bunch of bytes. If > you have multi-byte values (i.e. words) that you want to store, you must > figure that out yourself. The `htonl' and `ntohl' functions may be > helpful if what you want doesn't happen to match the CPU's spec. Continuing... Note that these functions don't necessarily change the endianness. If the CPU naturally stores numbers big-endain, these functions just return the number you pass. If the CPU is little-endian (or any other endianness) then the number is converted in such a way that when it's stored in memory it's in big-endian format. Either way, the result is that if you store the returned value to memory you are writing the value you passed, but in big-endian. With djgpp of course running on Intel processors, the functions do swap the byte order of the argument, since Intel processors are little-endian. So, you'll be fine if you're writing memory blocks to a file and want all numbers to be big-endian -- just pass them through the hton? filters before storing them in the memory block, and on reading them back pass them through ntoh? (? is `s' or `l'). If you have to store the numbers little-endian (or any other non-big endianness) then do the hton? conversion and then manually switch the byte order. -- george DOT foot AT merton DOT oxford DOT ac DOT uk xu do tavla fo la lojban -- http://xiron.pc.helsinki.fi/lojban/lojban.html