Binary file to C array(bin2c)

 1 /********************************************************************************
 2  *                          Binary file to C array(bin2c)
 3  *  说明:
 4  *      由于工作中需要将bmp文件数据转换成C数组,于是写了这个工具(bin2c),代码如你
 5  *      所见,只有看上去不多的几行.
 6  *
 7  *                                2015-4-20 周一 阴 深圳 南山 西丽平山村 曾剑锋
 8  *******************************************************************************/
 9 #include <stdio.h>
10 #include <string.h>
11 
12 int main ( int argc, char** argv )
13 {
14     int i = 0;            
15     char ch = '\0';        
16 
17     if ( 2 != argc ) {
18         printf( "\n    Usage: bin2c <file> \n\n" );
19         return -1;
20     }
21 
22     FILE *binfile = fopen( argv[1], "rb" );
23 
24     // get file name for array's name
25     while ( '.' != argv[1][i++] );
26     argv[1][ i-1 ] = 0;
27 
28     //get file data and change to const unsigned char array's data
29     i = 1;
30     printf( "const unsigned char %s[] = { \n\t", argv[1] );
31     while ( EOF != (ch = fgetc( binfile )) ) 
32          printf( "0x%02X%s\t", (unsigned char)ch , ( i++ % 8 ) == 0 ? "\n" : "" );
33     printf( "\n};\n" );
34 
35     fclose( binfile );
36 }

 

posted on 2015-04-21 13:03  zengjf  阅读(1045)  评论(0编辑  收藏  举报

导航