[SDL] Endian Conversion?

Sweeney, Steven (FNB) SweeneS at fnb.co.uk
Tue Jan 7 02:14:01 PST 2003


Thanks for the replies all :) I'm going to try the method below... Think I
may have confused some people here,

I mainly code on and compile Linux (x86 AMD, if it matters) and I also
compile for Windows too (Intel x86). I'm actually able to take the Linux
packed up data file and use it with the Windows binary no problems... so I
know it works there.

Hopefully using the method below will also allow me to do the same for PPC,
etc versions... It's basically to enable people who want to do ports to just
recompile the code without needing to pack up all the individual music,
graphics, data and sounds files again. But we'll see how it goes :)

Cheers,
Steve :))

> -----Original Message-----
> From:	Glenn Maynard [SMTP:g_sm at zewt.org]
> Sent:	Monday, January 06, 2003 9:04 PM
> To:	sdl at libsdl.org
> Subject:	Re: [SDL] Endian Conversion?
> 
> On Mon, Jan 06, 2003 at 09:50:07PM +0100, M. Egmond wrote:
> > unsigned char temp;
> > temp=value; /* value being the 32 bit integer */
> > fwrite(temp,1,1,f);
> > temp=((unsigned int)value)>>8;
> > fwrite(temp,1,1,f);
> > temp=((unsigned int)value)>>16;
> > fwrite(temp,1,1,f);
> > temp=((unsigned int)value)>>24;
> > fwrite(temp,1,1,f);
> 
> You probably meant
> fwrite(&temp, 1, 1, f);
> (pointer to the char)
> 
> This should work:
> 
> value = SDL_SwapLE32(value);
> fwrite(&value, sizeof(value), 1, f);
> 
> which converts the value to little-endian (if on a big endian machine)
> and writes it; read it back with:
> 
> fread(&value, sizeof(value), 1, f);
> value = SDL_SwapLE32(value);
> 
> Alternatively, use SDL_RWops:
> 
> SDL_WriteLE32(file, value);
> value = SDL_ReadLE32(file);
> 
> -- 
> Glenn Maynard
> 
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
> 
> 
> .sophos.3.64.01.02.




More information about the SDL mailing list