From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,a18c782bd6789eca X-Google-Attributes: gid1164f8,public X-Google-Thread: 1094ba,a18c782bd6789eca X-Google-Attributes: gid1094ba,public X-Google-ArrivalTime: 1994-11-14 16:46:23 PST Path: nntp.gmd.de!xlink.net!howland.reston.ans.net!gatech!concert!bigblue.oit.unc.edu!bigmax.bme.unc.edu!frey From: Eric Frey Newsgroups: comp.sys.dec,comp.lang.fortran Subject: Re: byte swapping to get data readable on a decstations Date: 14 Nov 1994 21:55:17 GMT Organization: UNC Deptartment of Biomedical Engineering Distribution: world Message-ID: <3a8mc5$1cld@bigblue.oit.unc.edu> References: <3a0u38$o32@panix2.panix.com> <39vdlj$944@panix2.panix.com> NNTP-Posting-Host: bigmax.bme.unc.edu Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Newsreader: Nuntius Version 1.3b15 X-XXMessage-ID: X-XXDate: Mon, 14 Nov 1994 21:54:19 GMT Xref: nntp.gmd.de comp.sys.dec:19668 comp.lang.fortran:15747 There were several errors in previous responses to this questions. First, note that the swapping that needs to be done for 4 byte datatypes is not the same as doing two swaps of the consectutive 2 byte elements. I.e., doing 2 swabs won!t work! Nor will using dd with convert=swab. You need to swap bytes 1 and 4 and 2 and 3, as pointed out in one of the posts. A stupid C program to accomplish this (one I use all the time) follows. I!m not sure that this will work for CRAYs, since I think they use (8 byte floats). -------- /* swapwords Descrpiton: this is a simple program for swapping the order of bytes in a binary files consiting oe either 4 byte integers or floating point values. Usage: swapwords infile outfile Revision history: written 10/25/89 eric frey */ #include #define SWAP(a,b,t) t = a; a = b; b = t main(argc,argv) int argc; char **argv; { int count=0; FILE *fpin, *fpout; char errbuf[256]; char buf[4];/* buffers whose bytes are to be swapped*/ char tmp; if (argc != 3){ fprintf(stderr,"usage: swapwords infile outfile\n"); exit(1); } if ((fpin = fopen(*++argv,"r")) == NULL){ sprintf(errbuf, "can't open %s for input", *argv); perror(errbuf); exit(1); } if ((fpout = fopen(*++argv,"w")) == NULL){ sprintf(errbuf, "can't open %s for output", *argv); perror(errbuf); } while(fread(buf,4,1,fpin) == 1){ count++; SWAP(buf[0], buf[3], tmp); SWAP(buf[1], buf[2], tmp); if (fwrite(buf,4,1,fpout) != 1){ perror("error writing output file"); exit(1); } } fprintf(stderr,"swapped %d 4 byte integers\n",count); return(0); }