[SDL] Endian Conversion?

Warren Young warren at etr-usa.com
Mon Jan 6 16:03:01 PST 2003


Sweeney, Steven (FNB) wrote:
> It runs fine on x86 machines (Windows and Linux). However, I'd like to be
> able to get the same file to run on other systems, like PPC. Basically the
> only thing it trips up on is the reading of the binary integer,

The "byte at a time" method will indeed work, but it's slower than 
reading the data 4 bytes at a time.  Instead, read the integer into 
memory and call ntohl() on it.  When writing the file out, use htonl() 
on it.  Every modern *ix system has these functions, as does Windows if 
you link to winsock.lib.  Since every Windows system since Windows 95 
comes with Winsock it's safe to rely on this family of functions. 
That's not totally true: winsock.dll isn't present on Windows when all 
networking is disabled, including dialup networking.  That's pretty rare 
these days.

I should point out that reading from a file 4 bytes at a time is slow, 
too.  Not as slow as reading 1 byte at a time, but....  Point is, you 
should be reading in 1K to 4K blocks at a time.  Do some benchmarks -- 
betcha find that it's a lot faster reading big chunks at once.  I 
realize this means you will usually have partial chunks of your 
variable-sized data blocks in memory at one time; this requires more 
clever coding to deal with, but it will net you a worthwhile speed 
improvement if your file reading code currently takes more than about a 
second to run.  (That will depend on the data size and the speed of your 
test machine.)





More information about the SDL mailing list