Mail Archives: djgpp/1999/08/16/16:45:02
George Ryot wrote:
> > > > No. Here's the right way to do it. This code is not edian safe.
> > > > sz = *(short*)(file_buffer+6);
> Me thinks some programmers just get stuck in hack mode determined to
> produce unsafe code for the sake of a few microseconds. ;-))
>
> How would Nick extract the byte pairs he needs using that last hack?
>
> The following (boring?) solution should do the trick:
>
> unsigned short sz, tz;
> sz = (file_buffer[5] << 8) & file_buffer[6];
> tz = (file_buffer[10] << 8) & file_buffer[11];
>
> Note that I have assumed you really do mean 6th, 7th, 11th & 12th
> bytes (file_buffer[0] would be the first byte) and that the 16-bit
> integers are stored high byte low byte. I have also assumed that the
> values are unsigned (always positive), things might get complicated
> otherwise. You can use int instead of short if you prefer.
I don't understand how your code is any less of a 'hack' than mine. Both are not
endian safe. Both will work consistantly on each individual machine (I'm not saying
that you could exchange data files and have them work). How is my code 'unsafe'?
--
-Rolf Campbell (39)3-6318
- Raw text -