Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!rpi!batcomputer!cornell!johnhlee From: johnhlee@CS.Cornell.EDU (John H. Lee) Newsgroups: comp.sys.amiga.programmer Subject: Re: 4 bytes to a long? Message-ID: <1991Jun11.214051.16596@cs.cornell.edu> Date: 11 Jun 91 21:40:51 GMT References: <55908@nigel.ee.udel.edu> Sender: news@cs.cornell.edu (USENET news user) Reply-To: johnhlee@cs.cornell.edu (John H. Lee) Organization: Cornell Univ. CS Dept, Ithaca NY 14853 Lines: 30 Nntp-Posting-Host: thialfi.cs.cornell.edu In article mykes@amiga0.SF-Bay.ORG (Mike Schwartz) writes: >In article <55908@nigel.ee.udel.edu> jleonard@pica.army.mil writes: >>mrimages@beach.gal.utexas.edu in <451.28517f01@beach.gal.utexas.edu> write: >> Is there a fast, simple way to convert 4 bytes into a long? >> >>I use : >> >>char ch[4]; >>long l; >> >>memcpy( (char *)&l, ch, sizeof(long)); > >If portability isn't a concern: > > ULONG l; > char *ch = &l; > >ch[0] is MSB of l, ch[3] is LSB. This is dependant on byte order of ULONG. A little clarification is in order. When you obtain the 4 bytes in the first place (from for instance, fgetc()), place in the sizeof(long)-byte buffer pointed to by "ch". You can then access the long through "l" without copying. However, if the 4 bytes are say, in the middle of some other buffer, you must still do a copy of the sizeof(long) bytes to ensure they end up properly long-word aligned. ------------------------------------------------------------------------------- The DiskDoctor threatens the crew! Next time on AmigaDos: The Next Generation. John Lee Internet: johnhlee@cs.cornell.edu The above opinions are those of the user, and not of this machine.