Mail Archives: djgpp/1999/08/16/16:47:45
Damian Yerrick wrote:
> > No. Here's the right way to do it. This code is not edian safe.
> > sz = *(short*)(file_buffer+6);
>
> /* That still isn't byte-order safe. The byte-order-safe way to do it is by
> shifting each individual byte. The following code works on all platforms,
> but make sure that file_buffer points to _unsigned_ char data or you'll get
> nasty sign extension that screws this up. */
>
> /* if file was written in big endian (Motorola-order) format */
> sz = ((short)file_buffer[6] << 8) + file_buffer[7];
>
> /* if file was written in little endian (Intel-order) format */
> sz = ((short)file_buffer[7] << 8) + file_buffer[6];
> This should clear things up.
But, that requires doing a whole bunch of #ifdef's to determine if the
current compiler uses BIGENDIAN or not. The easiest way would be to use the
lton() function (local to network) which changes the local byte-order to
network-byte-order (big-endian). I'm not sure if I have the function name
correct, but it is usually associated with TCP/IP stuff and comes with
libsocket for DJGPP. This way, all of the byte-order conversion is already
done for you and you don't have to worry about your platform.
--
-Rolf Campbell (39)3-6318
- Raw text -