Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cis.ohio-state.edu!sample.eng.ohio-state.edu!purdue!haven.umd.edu!uvaarpa!murdoch!turing!mbs From: mbs@turing.acs.virginia.edu (Michael B. Smith) Newsgroups: comp.sys.amiga.programmer Subject: Re: 4 bytes to a long? Message-ID: <1991Jun13.220554.19518@murdoch.acc.Virginia.EDU> Date: 13 Jun 91 22:05:54 GMT References: <451.28517f01@beach.gal.utexas.edu> <22408@cbmvax.commodore.com> Sender: usenet@murdoch.acc.Virginia.EDU Organization: University of Virginia Lines: 42 In article <22408@cbmvax.commodore.com> carolyn@cbmvax.commodore.com (Carolyn Scheppner - CATS) writes: >In article <451.28517f01@beach.gal.utexas.edu> mrimages@beach.gal.utexas.edu writes: >> >> Is there a fast, simple way to convert 4 bytes into a long? > >The IFF code has always done it this way: > >#define MAKE_ID(a,b,c,d) \ > ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d)) > >where a,b,c,and d are characters and the result is a long >-- >========================================================================== B > Carolyn Scheppner -- Tech. Mgr. CATS - Commodore Amiga Technical Support > PHONE 215-431-9180 {uunet,rutgers}!cbmvax!carolyn carolyn@commodore.com > > Calm down. It's just ones and zeros. >========================================================================== I use code similar to this, but have found that when a, b, c, & d are pointers that the lower 8 bits need to be masked. My example is the following code fragment: long make_long (p) char *p; { return ( ((*(p + 0) & 0xff)<<24) + ((*(p + 1) & 0xff)<<16) + ((*(p + 2) & 0xff)<<8 ) + ((*(p + 3) & 0xff) ) ); } I've always presumed (but never checked) that the compiler optmizes out the +0 construct (there for readability). I use this when I am reconstructing a file that I don't have a 'struct' for. And of course (since I'm new to this), this code could be buggy (especially since I don't do the casts) -- but it works for me.:) Michael B. Smith mbs@turing.acs.virginia.edu