Created
April 27, 2012 11:21
-
-
Save thommyj/2508488 to your computer and use it in GitHub Desktop.
reorder endian in 32bit words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
unsigned long bswap32 (unsigned long ul) | |
{ | |
return ((ul & 0x000000FF) << 24) | ((ul & 0x0000FF00) << 8) | | |
((ul & 0x00FF0000) >> 8) | ((ul & 0xFF000000) >> 24); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
FILE *from, *to; | |
unsigned int word; | |
if(argc!=3) { | |
printf("Usage: <source> <destination>\n"); | |
exit(1); | |
} | |
/* open source file */ | |
if((from = fopen(argv[1], "rb"))==NULL) { | |
printf("Cannot open source file.\n"); | |
exit(1); | |
} | |
/* open destination file */ | |
if((to = fopen(argv[2], "wb"))==NULL) { | |
printf("Cannot open destination file.\n"); | |
exit(1); | |
} | |
/* copy the file */ | |
do{ | |
bzero((void*)&word,sizeof(word)); | |
fread(&word,sizeof(word),1,from); | |
if(ferror(from)) { | |
printf("Error reading source file.\n"); | |
exit(1); | |
} | |
word = bswap32(word); | |
fwrite(&word,4,1,to); | |
if(ferror(to)) { | |
printf("Error writing destination file.\n"); | |
exit(1); | |
} | |
}while(!feof(from)); | |
fclose(from); | |
fclose(to); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment