zoukankan      html  css  js  c++  java
  • 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 = '';        
    16 
    17     if ( 2 != argc ) {
    18         printf( "
        Usage: bin2c <file> 
    
    " );
    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[] = { 
    	", argv[1] );
    31     while ( EOF != (ch = fgetc( binfile )) ) 
    32          printf( "0x%02X%s	", (unsigned char)ch , ( i++ % 8 ) == 0 ? "
    " : "" );
    33     printf( "
    };
    " );
    34 
    35     fclose( binfile );
    36 }
  • 相关阅读:
    MVC模式的学生信息增删改查
    常用排序算法
    2803 爱丽丝·玛格特罗依德
    3118 高精度练习之除法
    中秋练习题
    poj2011
    P1558 色板游戏
    P1830 轰炸III
    P1656 炸铁路
    1067 机器翻译
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/4444084.html
Copyright © 2011-2022 走看看