Xref: utzoo alt.sources:551 comp.sources.d:3583 Path: utzoo!attcan!uunet!wyse!vsi1!apple!bloom-beacon!tut.cis.ohio-state.edu!mailrus!uflorida!novavax!twwells!bill From: bill@twwells.uucp (T. William Wells) Newsgroups: alt.sources,comp.sources.d Subject: Re: Auto byte swapping (was Re: fixed unzip) Message-ID: <873@twwells.uucp> Date: 27 Apr 89 05:27:08 GMT References: <6778@c3pe.UUCP> Reply-To: bill@twwells.UUCP (T. William Wells) Followup-To: comp.sources.d Organization: None, Ft. Lauderdale Lines: 29 Expires: Sender: Distribution: Keywords: 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