1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <cstdio>
void PrintRow(FILE* dst,const unsigned char* buf,unsigned count)
{
fprintf(dst,"\n ");
while(count > 1)
{
fprintf(dst,"0x%02X,",*buf);
++buf;
--count;
}
if(count > 0)
fprintf(dst,"0x%02X",*buf);
}
void BinToCpp(
FILE* src, // source file, opened with "rb"
FILE* dst) // dest file, opened with "wt" note neither file is closed by this
{
// determine source file size
unsigned srcsize;
fseek(src,0,SEEK_END);
srcsize = (unsigned)ftell(src);
fseek(src,0,SEEK_SET);
// dump source file size to output file
fprintf(dst,"\n\nconst unsigned FILE_SIZE = %u;\n\n",srcsize);
//
fprintf(dst,"const unsigned char FILE_DATA[] = {");
// take the source file in 16 byte blocks
unsigned char buf[16];
while(srcsize > 16)
{
fread(buf,1,16,src);
PrintRow(dst,buf,16);
fprintf(dst,",");
srcsize -= 16;
}
// do the rest of the file
fread(buf,1,srcsize,src);
PrintRow(dst,buf,srcsize);
fprintf(dst,"\n};\n\n");
}
|