Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!cucstud!c3pe!charles From: charles@c3pe.UUCP (Charles Green) Newsgroups: alt.sources Subject: Auto byte swapping (was Re: fixed unzip) Summary: union char short long Message-ID: <6778@c3pe.UUCP> Date: 24 Apr 89 02:01:07 GMT References: <27409@conexch.UUCP> <14820003@hpsmtc1.HP.COM> Reply-To: charles@c3pe.UUCP (Charles Green) Organization: C3 Incorporated, Herndon, VA Lines: 17 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.