Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
From: Eric Frey <frey@bme.unc.edu>
Subject: Re: byte swapping to get data readable on a decstations
Date: 14 Nov 1994 21:55:17 GMT	[thread overview]
Message-ID: <3a8mc5$1cld@bigblue.oit.unc.edu> (raw)
In-Reply-To: 39vdlj$944@panix2.panix.com

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 <stdio.h>

#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);
}


  reply	other threads:[~1994-11-14 21:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-11-10 13:44 byte swapping to get data readable on a decstations Edward Hartnett
1994-11-10 16:07 ` Steve Lionel
1994-11-10 16:47 ` Chuck Campbell
1994-11-11  9:31 ` Ken Bell
1994-11-14 21:55   ` Eric Frey [this message]
1994-11-11 23:18 ` Ken Bell
1994-11-14 14:19 ` Roger Grady
1994-11-16  4:06 ` byte swapping to get data readable on a dec John Lovell
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox