EDIT: After doing some more research, it seems that what i actually need is big endian to middle endian and vice versa. so 12345678 -> 34127856 and back. Sorry for any confusion.
I have a small file, exactly 16MB. I am reading the whole file into a buffer. What Im trying to do is byteswap the whole file/buffer in one go if possible (eg. uniformly/globaly swaps ADDECEFA => DEADFACE). I have read countless pages of byteswapping but alot of the bitwise/byteswap stuff goes right over my head for some reason (dumb brain most likely). If anyone knows a bitwise/byteswap for dummies, please point me in the right direction!
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
const char * suffix = ".reversed";
FILE *input = fopen(argv[1], "rb");
char * out = strcat(argv[1], suffix);
FILE *output = fopen(out, "wb");
int data[16384];
int swapped;
while(fread(data,sizeof data, 1, input)){
swapped = ((data & 0x000000FF) << 24) |
((data & 0x0000FF00) << 8) |
((data & 0x00FF0000) >> 8) |
((data & 0xFF000000) >> 24);
fwrite(swapped, sizeof data , 1, output);
/* by golly it copies the file fast as heck!
but i am unsure how to manipulate 'data' buffer
so as it uniformly/globaly swaps ADDECEFA => DEADFACE
*/
}
}
Also, if you see me doing something wrong in my code, please tell me and show me a better way. If you need me to elaborate anything please dont hesitate to ask. Thanks and have a good one.
EDIT: I added one of my failed attempts. My end goal was to swap the bytes as I write it back out to outfile and swap them as efficiently as possible.