Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!pdn!palan!ckctpa!crash From: crash@ckctpa.UUCP (Frank J. Edwards) Newsgroups: comp.sys.amiga.programmer Subject: Re: 4 bytes to a long? Message-ID: <1991Jun14.161152.1421@ckctpa.UUCP> Date: 14 Jun 91 16:11:52 GMT References: <55908@nigel.ee.udel.edu> <19488689.ARN0eac@swinjm.UUCP> Organization: Edwards & Edwards Consulting Lines: 35 In article <19488689.ARN0eac@swinjm.UUCP> forgeas@swinjm.UUCP (Jean-Michel Forgeas) 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)); > >To convert four bytes into a long: > l = *((long*) ch) >This will generate only one instruction: > move.l _ch,_l Yes, but watch out for alignment restrictions. To take care of that, put the "long l" before the "char ch". On stack-oriented machines, *usually* the long will be allocated on the stack first and then the char array, so the array must be properly aligned. A better method, though, is to use a union: union { char ch[4]; long l; } uvar; Now, put the individual bytes into uvar.ch[0] through uvar.ch[3] and then read'em back as uvar.l >Jean-Michel Forgeas -- Frank J. Edwards | "I did make up my own mind -- there 2677 Arjay Court | simply WASN'T ANY OTHER choice!" Palm Harbor, FL 34684-4504 | -- Me Phone (813) 786-3675 (voice) | Only Amiga Makes It Possible...