Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!wuarchive!mit-eddie!uw-beaver!zephyr.ens.tek.com!tektronix!reed!pzbaum!omepd!merlyn From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.c Subject: Re: determining endian ? Message-ID: <5288@omepd.UUCP> Date: 29 Nov 89 16:29:54 GMT References: <858@rna.UUCP> Sender: news@omepd.UUCP Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Hillsboro, Oregon, USA Lines: 39 In-reply-to: dan@rna.UUCP (Dan Ts'o) In article <858@rna.UUCP>, dan@rna (Dan Ts'o) writes: | I'm sure this is old hat, but I recently needed to figure out if a | particular host machine was big-endian or little endian. Comments, please, | on this little snippet of code. [code deleted] Well, Larry Wall uses the following piece of code in his marvelous Configure script (created with metaConfigure, of course). The output of the script gives the byte order as an integer, little-endian ending up as 1234, big endian as 4321, and so on. -------------------------------------------------- cut here #include main() { int i; union { unsigned long l; char c[sizeof(long)]; } u; if (sizeof(long) > 4) u.l = 0x0807060504030201; else u.l = 0x04030201; for (i=0; i < sizeof(long); i++) printf("%c",u.c[i]+'0'); printf("\n"); } -------------------------------------------------- cut here You could probably build your thing on top of this. Just another C hacker, -- /== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\ | on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn | \== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/