binsrc.c

来自「dm642网络传输程序」· C语言 代码 · 共 65 行

C
65
字号
#include <stdio.h>
#include <ctype.h>

unsigned char buffer[0x2000];

main(argc,argv)
int argc;
char *argv[];
{
    FILE          *infile,*outfile;
    int           tmp = 0;
    long          size,ltmp;
    unsigned char *pc;

    if (argc != 4)
    {
       printf("\nUsage: BINSRC [InFile] [OutFile] [Name]\n");
       exit(1);
    }

    if (!(infile = fopen(argv[1],"rb")))
        { printf("Can't open source file: %s\n",argv[1]); exit(1); }

    // Get the file size
    fseek( infile, 0, SEEK_END );
    size = ftell( infile );
    rewind( infile );

    printf("Input File : %s\n",argv[1]);
    printf("File Size  : %ld\n",size);

    if (!(outfile = fopen(argv[2],"wb")))
        { printf("Can't open output file: %s\n",argv[2]); exit(1); }

    printf("Output File: %s\n",argv[2]);

    fprintf( outfile, "#define %s_SIZE %ld\r\n", argv[3], size );
    fprintf( outfile, "unsigned char %s[] = {\r\n    ", argv[3] );
    tmp = 0;
    while( size )
    {
        if( size >= sizeof(buffer) )
            ltmp = sizeof(buffer);
        else
            ltmp = size;
        fread(buffer,1,(short)ltmp,infile);

        pc = buffer;
        size -= ltmp;
        while( ltmp-- )
        {
            if( size || ltmp )
            {
                fprintf(outfile,"0x%02X, ",*pc++);
                if( !(++tmp % 12) )
                    fprintf(outfile,"\r\n    ");
            }
            else
                fprintf(outfile,"0x%02X };\r\n",*pc++);
        }
    }
    fclose(infile);
    fclose(outfile);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?