From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,36cb37ca074b2135 X-Google-Attributes: gidfee84,public From: mbt@ast.cam.ac.uk (Mark Taylor) Subject: Re: Reading sun binaries on DEC-Alpha f77? Date: 1998/08/20 Message-ID: <6rh3nm$bre$1@pegasus.csx.cam.ac.uk>#1/1 X-Deja-AN: 382966668 References: Organization: University of Cambridge, England Newsgroups: comp.unix.osf.osf1 Joerg Lippmann (jlippmann@ifm.uni-kiel.de) wrote: : Hi! : Is there an easy way to read sun binary data files with DEC-Alpha : f77-programs? I knocked up the following short C program to convert files from little-endian to big-endian or the other way round. Note it will only work if the file has nothing but 4-byte words in (e.g. all REALs or INTEGERs). #include /* This is a very simple program to swap 4-byte words from * big-endian to little-endian (or vice versa). * Useful, e.g., for moving REALs or INTEGERS between Suns and DECs. * Note that this only works in virtue of the fact that both * use the same representation for REALs and INTEGERs - at least * this is true at Digital Unix 4.0c and Solaris 2.6. * However there is no guarantee that this will hold for arbitrary * operating systems with differing byte-order. For a more general * solution one should use XDR or something. */ int main (int argc, char *argv[]) { char buf[4], buf2[4]; size_t b; FILE *in, *out; if ((argc > 3) || (argc > 1 && *argv[1] == '-')) { printf ("Usage: %s [ infile [ outfile ] ]\n", argv[0]); exit (1); } in = (argc >= 2) ? fopen (argv[1], "rb") : fdopen (0, "rb"); if (!in) { perror (argv[0]); exit (1); } out = (argc == 3) ? fopen (argv[2], "wb") : fdopen (1, "wb"); if (!out) { perror (argv[0]); exit (1); } while (fread (buf, 4, 1, in) == 1) { buf2[0] = buf[3]; buf2[1] = buf[2]; buf2[2] = buf[1]; buf2[3] = buf[0]; if (fwrite (buf2, 4, 1, out) != 1) { printf ("%s: Error writing output file\n", argv[0]); exit (1); } } if (ferror(in) != 0) { printf ("%s: Error reading input file\n", argv[0]); exit(1); } } --------------------------------------------------------------------------- | Mark Taylor Institute of Astronomy, Cambridge, UK | | mbt@ast.cam.ac.uk http://dougal.chm.bris.ac.uk/~mbt/ 01223 337535 | ---------------------------------------------------------------------------