Here is a small C routine to copy a file swapping the bytes, i.e.,
changing the endian-ness.
Some (well at least one, Intel) Fortran compilers have a
mechanism for changing the endian-ness when reading or writing a file.
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);
}
|