[alt.sources] Auto byte swapping

charles@c3pe.UUCP (Charles Green) (04/24/89)

In article <14820003@hpsmtc1.HP.COM> swh@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.

bill@twwells.uucp (T. William Wells) (04/27/89)

Note that followups have been directed to comp.sources.d.

In article <6778@c3pe.UUCP> charles@c3pe.UUCP (Charles Green) writes:
: 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.

Don't do this!

It doesn't work unless shorts are two characters long; there are
plenty of systems where this isn't so.

If you have to read two byte integers from a file, do it a byte at a
time and write an expression to combine the bytes. This is the only
portable way to do it.

---
Bill                            { uunet | novavax } !twwells!bill

jbrown@jato.Jpl.Nasa.Gov (Jordan Brown) (04/28/89)

In article <6778@c3pe.UUCP> charles@c3pe.UUCP (Charles Green) writes:
>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.

There's an even better way.  Keep a tag in your file (or net packet) which
indicates your byte order.  Whenever you create a file (or packet), stuff
0x0102 into this "order" word.  Whever you read one of these files, look
at the "order" word.  If it's 0x0102, you don't need to swap anything.
If it's 0x0201, then swap everything.  This works on either order; you
don't need to know what you are or what the originator was.  It also saves
swapping completely when the writer and reader are the same machine type,
which is presumably a common case.

If you want compatibility with the Ritchie PDP-11 compiler, which stores
longs as middle-high, high, low, middle-low (rather than Vax low, middle-low,
middle-high, high), then use a 4-byte tag.  The exact algorithm is left
as an exercise for the reader.