C之bin2rev

使用c语言将bin文件的大小端反转:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>

#define SIZE    4

int main(int argc, char *argv)
{
        FILE *fp_in = NULL;
        FILE *fp_out = NULL;
        unsigned char dat[SIZE];
        unsigned char buf[SIZE];

        char path_buffer[_MAX_PATH];
        char drive[_MAX_DRIVE];
        char dir[_MAX_DIR];
        char filename[_MAX_FNAME];
        char ext[_MAX_EXT];
        char file_to_write[_MAX_FNAME + _MAX_EXT];

        if (NULL != argv[1]) {
                fp_in = fopen(argv[1], "rb");
        }

        if (NULL == fp_in) {
                printf("No such file!\n");
        } else {
                _splitpath(argv[1], drive, dir, filename, ext);

                strcpy(file_to_write, filename);
                strcat(file_to_write, ".rev");

                fp_out = fopen(file_to_write, "wb");
                if (NULL == fp_out) {
                        printf("No such file!\n")
                }

                fread(buf, 1, SIZE, fp_in);
                while(!feof(fp_in)) {
                        for (int k = 0; k < SIZE/4; k ++) {
                                dat[4 * k + 0] = buf[4 * k + 3];
                                dat[4 * k + 1] = buf[4 * k + 2];
                                dat[4 * k + 2] = buf[4 * k + 1];
                                dat[4 * k + 3] = buf[4 * k + 0];
                        }

                        fwrite(dat, 1, SIZE, fp_out);
                        fread(buf, 1, SIZE, fp_in);
                }

                fclose(fp_in);
                fclose(fp_out);
        }

        return 0;
}

以4字节为单位进行高低字节反转,将bin文件拖拽到bin2rev.exe上或使用bat批处理执行带参数的exe程序,即可得到带.rev的bin文件

猜你喜欢

转载自blog.csdn.net/u011958166/article/details/78672658
rev