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 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值