如何将一个二进制的xxx.bin文件轻松转为C语言数组

简介:     今天在写一个SPI-flash读写程序,目的是要将一个二进制文件写到SPI_FLASH中,最后通过开机读取,实际上这个.bin文件就是uboot和second-boot的结合体。通过SD卡写到SPI-FLASH中就可以脱离SD卡开机启动了。

    今天在写一个SPI-flash读写程序,目的是要将一个二进制文件写到SPI_FLASH中,最后通过开机读取,实际上这个.bin文件就是uboot和second-boot的结合体。通过SD卡写到SPI-FLASH中就可以脱离SD卡开机启动了。

    这个程序的后半部分参考了以前的文章:http://blog.csdn.net/morixinguan/article/details/50646738

    如何给文件产生空洞文件。

    为什么需要将.bin文件转化为数组?因为.bin文件的大小有的几M,甚至是几百块,对于以字节为单位的数组来说的确是太庞大了,所以我参考了网上一些相关的程序,独自写了一个出来。用法如下:

/*
Date:2016.12.16
author:杨源鑫
*/
按照提示输入:
当前目录下的bin文件的文件名
ep : xxx.bin
接着输入:
要生成的.h文件的名称: 
ep : xxx.h


会在目录下自动生成.h文件:
.h文件内包括两个数组
一个名称是SPIflashimage,这个就是从.bin文件中读出来的数组。
另一个是预留的空数组mfgimage,这个可以作为清数组的时候用,当然可以自由去改大小。
源程序如下:

/*
将二进制转化成数组头文件 
*/
//Version:2016.12.16
//author:Y.X.YANG
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
typedef unsigned char u8;
typedef unsigned int  u32;
void read_bin(char *path, u8 *buf, u8 size)  
{  
    FILE *infile;  
      
    if((infile=fopen(path,"rb"))==NULL)  
    {  
        printf( "\nCan not open the path: %s \n", path);  
        exit(-1);  
    }  
    fread(buf, sizeof(u8), size, infile);
    fclose(infile);  
} 
u32 GetBinSize(char *filename)  
{     
    u32  siz = 0;     
    FILE  *fp = fopen(filename, "rb");     
    if (fp)   
    {        
        fseek(fp, 0, SEEK_END);        
        siz = ftell(fp);        
        fclose(fp);     
    }     
    return siz;  
} 
void OutPut_handle(char *outpath, u8 *buf, u32 size)  
{  
    FILE *infile; 
    int i,j,k,n;  
    int fd ;
    char pbuf[10]={0};  
    char mfgimage[4096*2];
    char *array = "static const unsigned char SPIflashimage[SPIIMAGESIZE] = {\n";
    char *array1 = "static const unsigned char mfgimage[MFGIMAGESIZE] = {\n";
    char *Handle = "#ifndef SPI_FLASH_H_ \n";
    char *Handle1 = "#define SPI_FLASH_H_ \n";
    char *SPI_SPIflash = "#define SPI_SPIflash 0 \n";
    char *SPIIMAGESIZE = "#define SPIIMAGESIZE   411652 \n";
    char *MFGIMAGESIZE = "#define MFGIMAGESIZE   411652 \n";
    char *SIZE_4K      = "#define SIZE_4K   4096*2 \n";
    char *line_T       = "\n";
    char *EndIF        = "\n#endif \n";

    if((infile=fopen(outpath,"wa+"))==NULL)  
    {  
        printf( "\nCan not open the path: %s \n", outpath);  
        exit(-1);  
    }  
    k=0; 
    fwrite(Handle,strlen(Handle),1,infile);
    fwrite(Handle1,strlen(Handle1),1,infile);
    fwrite(SPI_SPIflash,strlen(SPI_SPIflash),1,infile);
    fwrite(SPIIMAGESIZE,strlen(SPIIMAGESIZE),1,infile);
    fwrite(MFGIMAGESIZE,strlen(MFGIMAGESIZE),1,infile);
    fwrite(SIZE_4K,strlen(SIZE_4K),1,infile);
    fwrite(array,strlen(array),1,infile);  
    for(i = 0; i < size; i++)  
    {  
            k++;  
		   sprintf(pbuf,"0x%02x",buf[i]);  
            fwrite(pbuf,strlen(pbuf),1,infile);  
            if(k != 16)  
               fwrite(", ",strlen(", "),1,infile);  
            else  
               fwrite(",",strlen(","),1,infile);     
            if(k==16)  
            {  
                k=0;  
                fwrite("\n",strlen("\n"),1,infile);  
            }  
    }  
    fseek(infile,0,SEEK_END);  
    if(k == 0)  
        fwrite("};",strlen("};"),1,infile);  
    else  
        fwrite("\n};",strlen("\n};"),1,infile);         
    //在infile文件中和换行 
    fwrite(line_T,strlen(line_T),1,infile);
    //创建一个文件用于保存零数组 
    fd = creat("nufile.bin",0777);
	if(-1 == fd)
	{
		perror("creat fair!");
		return ;
 	}	 
 	//偏移写空 
	int offset = lseek(fd,4096*2,SEEK_END);
	write(fd,"",1);
	/**************************************************/
	//清数组 
	for(i = 0 ; i < 10 ; i++)
	   pbuf[i] = 0 ;
	for(i = 0 ; i < 4096*2 ; i++)
	   mfgimage[i] = 0 ;
	//写第二个数组 
    fwrite(array1,strlen(array1),1,infile);
    //从空文件里读数据读到mfgimage数组 
	read(fd,mfgimage,4096*2);
	//关闭文件句柄 
	close(fd);
	//往文件后面继续写数据 
	k = 0 ;
	for(i = 0; i < 4096*2; i++)  
    {  
           k++;  
		   sprintf(pbuf,"0x%02x",mfgimage[i]);  
           fwrite(pbuf,strlen(pbuf),1,infile);  
           if(k != 16)  
               fwrite(", ",strlen(", "),1,infile);  
           else  
               fwrite(",",strlen(","),1,infile);     
           if(k==16)  
           {  
              k=0;  
              fwrite("\n",strlen("\n"),1,infile);  
           }  
    }  
    fseek(infile,0,SEEK_END);  
    if(k == 0)  
        fwrite("};",strlen("};"),1,infile);  
    else  
        fwrite("\n};",strlen("\n};"),1,infile);
	
	fwrite(line_T,strlen(line_T),1,infile);
    fwrite(EndIF,strlen(EndIF),1,infile);  
    //删除当前目录下的一个空洞文件 
    if(remove("nufile.bin") == 0)
    	printf("del file success!\n");
    else
        printf("del file fair!\n");
    fclose(infile);  
}   
 
int main()  
{ 
	u8 *buf = NULL;  
	u32 size;  
	char srcbin[100]={0};  
	char dstfile[100]={0};  
	//读取目标.bin文件 
	printf("please input src file path\n");  
	scanf("%s",srcbin);
	//创建一个.h头文件用于保存bin转C数组的文件 
	printf("please input output path\n");
	scanf("%s",dstfile);
	//获取文件的大小 
	size = GetBinSize(srcbin);  
	//申请用于存放该文件的数组 
	buf = (unsigned char *)malloc(sizeof(unsigned char)*size);    
	//读取文件 
	read_bin(srcbin, buf, size);  	  
	//制作头文件,该头文件下含有两个数组,一个是有数据的,另外一个是全0数组
	//全0主要备用,以后要清空可以调用这个数组 
	OutPut_handle(dstfile, buf, size);  
    return 0;  
}  

执行结果:

生成的.h内容如下,太多了,我只截取一部分出来:

#ifndef SPI_FLASH_H_ 
#define SPI_FLASH_H_ 
#define SPI_SPIflash 0 
#define SPIIMAGESIZE   411652 
#define MFGIMAGESIZE   411652 
#define SIZE_4K   4096*2 
static const unsigned char SPIflashimage[SPIIMAGESIZE] = {
0x18, 0xf0, 0x9f, 0xe5, 0x18, 0xf0, 0x9f, 0xe5, 0x18, 0xf0, 0x9f, 0xe5, 0x18, 0xf0, 0x9f, 0xe5,
0x18, 0xf0, 0x9f, 0xe5, 0x18, 0xf0, 0x9f, 0xe5, 0x18, 0xf0, 0x9f, 0xe5, 0x18, 0xf0, 0x9f, 0xe5,
0x00, 0x02, 0xff, 0xff, 0x04, 0x02, 0xff, 0xff, 0x08, 0x02, 0xff, 0xff, 0x0c, 0x02, 0xff, 0xff,
0x10, 0x02, 0xff, 0xff, 0x14, 0x02, 0xff, 0xff, 0x18, 0x02, 0xff, 0xff, 0x1c, 0x02, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x07, 0x00, 0x00, 0x00,};
static const unsigned char mfgimage[MFGIMAGESIZE] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};

#endif
目录
相关文章
|
13天前
|
Linux C语言
C语言获取文件长度
C语言获取文件长度
|
28天前
|
安全 算法 程序员
【C/C++ 文件操作】深入理解C语言中的文件锁定机制
【C/C++ 文件操作】深入理解C语言中的文件锁定机制
31 0
|
1月前
|
C语言
在C语言中数组作为函数参数的应用与示例
在C语言中数组作为函数参数的应用与示例
15 0
|
1月前
|
C语言
【进阶C语言】数组笔试题解析
【进阶C语言】数组笔试题解析
17 0
|
1月前
|
存储 C语言 索引
C语言数组
C语言数组
14 0
|
1月前
|
C语言
C语言对文件的输入输出
C语言对文件的输入输出
17 0
|
1月前
|
存储 算法 数据挖掘
C语言中如何快速找出数组最大值下标
C语言中如何快速找出数组最大值下标
|
20天前
|
编译器 程序员 C语言
【C语言】变长数组,二分查找和数组之间自动替换的实现
【C语言】变长数组,二分查找和数组之间自动替换的实现
|
20天前
|
存储 C语言
【C语言数组】创建、初始化、以及使用2
【C语言数组】创建、初始化、以及使用
|
1月前
|
存储 程序员 C语言
C语言中的结构体数组
C语言中的结构体数组
9 0