/********************************************************************************
* Binary file to C array(bin2c)
* 说明:
* 由于工作中需要将bmp文件数据转换成C数组,于是写了这个工具(bin2c),代码如你
* 所见,只有看上去不多的几行.
*
* 2015-4-20 周一 阴 深圳 南山 西丽平山村 曾剑锋
*******************************************************************************/
#include <stdio.h>
#include <string.h>
int main ( int argc, char** argv )
{
int i = ;
char ch = '\0';
if ( != argc ) {
printf( "\n Usage: bin2c <file> \n\n" );
return -;
}
FILE *binfile = fopen( argv[], "rb" );
// get file name for array's name
while ( '.' != argv[][i++] );
argv[][ i- ] = ;
//get file data and change to const unsigned char array's data
i = ;
printf( "const unsigned char %s[] = { \n\t", argv[] );
while ( EOF != (ch = fgetc( binfile )) )
printf( "0x%02X%s\t", (unsigned char)ch , ( i++ % ) == ? "\n" : "" );
printf( "\n};\n" );
fclose( binfile );
}