Auto byte swapping (was Re: fixed unzip)

Charles Green charles at c3pe.UUCP
Mon Apr 24 12:01:07 AEST 1989


In article <14820003 at hpsmtc1.HP.COM> swh at hpsmtc1.HP.COM (Steve Harrold) writes:
>Thank you for the "byte-swapped" and "nibble" corrections.

I'd like to throw in my asynchronous $0.02 worth here.  I've had to write
programs which run on both architectures, and which use files on their own
and the "other" machine.  My favorite way to deal with this is to declare a

	union {
		char c[2],
		short s;
	} swab;

and stuff, say, a 0x0102 into swab.s and see what winds up in c[0] to find
out which type of machine you're on.  If you're dealing with an existing
file, you read the magic into swab.s and, depending on what shows up in
c[0], you know whether it's "your" format or not, and can swap bytes as
required in your program to compensate.



More information about the Alt.sources mailing list