Print

Print



W.J. Metzger wrote:

>Here is a small C routine to copy a file swapping the bytes, i.e.,
>changing the endian-ness.
>

The size of the cell within which you reverse the bytes depends on the
I/O list item.  If you are reading an 8-byte real, you reverse a block
of 8 bytes.  If you are reading a 2-byte integer, only 2 bytes are
reversed.  In general a file contains a mixture of different sizes of
data, so a simplistic program like the one below has pretty limited use.
(It is also not very optimal code for most modern processors, but that's
an unrelated issue.)

>
>Some (well at least one, Intel) Fortran compilers have a
>mechanism for changing the endian-ness when reading or writing a file.
>

I'd guess "some" is really "several".  This is the right way to do it.
The I/O operation knows, on an item by item basis, the correct size of
the block to be reversed.

Cheers,
Bill


>
>Wes
>
>On Tue, 2 Mar 2004, Joerg Stiller wrote:
>
>
>
>>Hello,
>>there seems to be a tradeoff between formatted and unformatted IO concerning
>>speed and portability. Do you know,
>>- what speed-up can be expected by using unformatted instead of
>>formatted  read/write
>>- wether there is a standard way to convert between  Big and Little
>>Endian formats?
>>
>>
>
>--
>Dr. W. J. Metzger            Experimental High Energy Physics Group
>tel. +31-24-3653127          Faculty of Natural Sciences
>     +31-24-3652099 (secr.)  University of Nijmegen (**)
>fax. +31-24-3652191          Toernooiveld 1
>                             6525 ED  Nijmegen,  The Netherlands
>e-mail:  [log in to unmask]      or   [log in to unmask]
>http://home.cern.ch/metzger/ or   http://www.hef.kun.nl/~wes
>  (**)  After 1 Sept. 2004,  Radboud University Nijmegen
>
>
>------------------------------------------------------------------------
>
>#include <stdio.h>
>#include <stdlib.h>
>int main(int argc, char **argv)
>{
>  FILE *input_file, *output_file;
>  char *input_name, *output_name;
>  unsigned int my_word;
>
>  if (argc < 3)
>    {
>      printf("Not enough arguments!\n");
>      printf("Usage: input output \n");
>      exit(0);
>    }
>  else if (argc > 3)
>    {
>      printf("Too many arguments!\n");
>      printf("Usage: input output \n");
>      exit(0);
>    }
>
>  input_name  = argv[1];
>  output_name = argv[2];
>
>  input_file  = fopen( input_name,"r");
>  output_file = fopen(output_name,"w");
>
>  while (feof(input_file) == 0)
>   {
>     char *b;
>     char temp;
>     my_word = getw(input_file);
>     if (feof(input_file) == 0)
>        {
>/*   printf ("%x ",my_word);*/
>     /* 0xabcd -> 0xdcba */
>     b = (char *) &my_word;
>     temp = b[0];
>     b[0] = b[3];
>     b[3] = temp;
>     temp = b[1];
>     b[1] = b[2];
>     b[2] = temp;
>/*   printf ("%x \n",my_word);*/
>
>     putw(my_word, output_file);
>        }
>/*     else*/
>/*        {*/
>/*        char a = 0x0a ;*/
>/*        printf ("eof\n");*/
>/*        putc(a, output_file);*/
>/*        }*/
>   }
>  fclose( input_file);
>  fclose(output_file);
>}
>
>

--
Bill Long                                   [log in to unmask]
Fortran Technical Support    &              voice: 651-605-9024
Bioinformatics Software Development         fax:   651-605-9142
Cray Inc., 1340 Mendota Heights Rd., Mendota Heights, MN, 55120