Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!usc!rpi!zaphod.mps.ohio-state.edu!lavaca.uh.edu!menudo.uh.edu!sugar!ficc!peter From: peter@ficc.ferranti.com (Peter da Silva) Newsgroups: comp.lang.c Subject: Re: Portability vs. Endianness Message-ID: Date: 14 Mar 91 14:59:29 GMT References: <1991Mar12.105451.19488@dit.upm.es> Reply-To: peter@ficc.ferranti.com (Peter da Silva) Organization: Xenix Support, FICC Lines: 32 In article <1991Mar12.105451.19488@dit.upm.es> esink@turia.dit.upm.es () writes: > long var; > unsigned char Bytes[4]; > > Is there a portable way to move the value held in var > into the memory space pointed to by Bytes, with the restriction > that the representation be in Most Significant Byte first > format ? I am well aware that sizeof(long) may not be 4. I > want the value contained in var converted to a 68000 > long word, and I want the code fragment to run on any > machine. The solution must be ANSI C. Bytes[0] = (var & 0xFF000000) >> 24; Bytes[1] = (var & 0x00FF0000) >> 16; Bytes[2] = (var & 0x0000FF00) >> 8; Bytes[3] = (var & 0x000000FF) >> 0; If you don't mind checking ifdefs, you can run a test program to check endianness and do this: #ifdef E4321 *((long *)Bytes) = var #else ... above code ... #endif One guy here wrote a fairly extensive .h file for manipulating things in 68000 order. It needs a single #define for the native byte order and defines everything else based on that. -- Peter da Silva. `-_-' peter@ferranti.com +1 713 274 5180. 'U` "Have you hugged your wolf today?"