如何将一个二进制的xxx.bin文件轻松转为C语言数组
今天在写一个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
如何将一个二进制的xxx.bin文件轻松转为C语言数组的更多相关文章
- .axf 转化 .bin文件 的方法
按住shift 右击按键,进入在 X:\Program Files\Keil\MDK510\ARM\ARMCC\bin . 中打开命令cmd.exe ,然后进入一下操作. 编译自己的工程,并将&quo ...
- 关于在VI中查看BIN文件二进制值不对的问题
通常,我们在vim中,可以使用命令 %!xxd 来查看文件对应的二进制值.但是最近发生了一个事情,查看到的BIN文件二进制值和直接用hexdump打印出来的不一样. 经过检查定位,发现是因为vimrc ...
- c代码写数据到二进制的bin文件中
需要将数据写入到bin文件中,打开该文件是一堆乱码,增加数据的保密性 例如:要写入的数据为一个字符串,加上若干个int型整数 #define _CRT_SECURE_NO_WARNINGS #incl ...
- STM32 Unicode 与 GBK 转换 .bin文件放到SD卡是啥意思
2个数组 : }; }; 一个是Unicode 编码,一个是GBK编码: 用c2b软件转成.bin 二进制文件放到SD卡里: SD卡放入字库 .FON STM32 代码: 代码中SD卡字库和二进制路径 ...
- ARM的BIN文件反汇编方法
最近在调试uboot的代码时,用的新版本的uboot,lowlevel_init函数里是空的,而且在链接文件中也没有发现对lowlevel_init.o的链接.在bl lowlevel_init 之前 ...
- KEIL_MDK生成Bin文件
1.MDK配置 MDK是使用安装目录下的(formelf.exe)工具来生成bin文件,配置方法:勾选 "Run # 1",在后面输入框写入bin文件生成方式 2.绝对路径 &qu ...
- hex文件和bin文件区别
HEX文件和BIN文件是我们经常碰到的2种文件格式.因为自己也是新手,所以一直对这两个文件懵懵懂懂,不甚了解,最近在做STM32单片机的IAP更新,其中要考虑HEX文件和BIN文件,所以需要学习下这两 ...
- shell脚本报错:-bash: xxx: /bin/bash^M: bad interpreter: No such file or directory
当我们把文件从windows系统中编辑的文件拷贝到linux系统中,如果我们执行文件会保存如下的错: shell脚本报错:-bash: xxx: /bin/bash^M: bad interprete ...
- Keil如何生成bin文件【Keil生成Bin文件的方法】
使用过Keil的同鞋都知道,现在Keil中默认可以输出.axf的调试文件和可以通过钩选输出的.hex可执行文件,没有bin(二进制)文件的输出选项.可是偏偏某些时候需要或者习惯性的使用.bin文件来进 ...
随机推荐
- C#之FTP上传下载(二)
这个类几乎包含了对FTP常用的方法,有不对的地方,欢迎批评指正 public class FtpClient { #region 构造函数 /// <summary> /// 创建FTP工 ...
- Mysql运算符与函数(胖胖老师)
use test;create table `employee`( emp_no int unsigned, emp_name varchar(30), emp_sex varcha ...
- python基础面试
1 请用自己的算法, 按升序合并如下两个list, 并去除重复的元素: list1 = [2, 3, 8, 4, 9, 5, 6]list2 = [5, 6, 10, 17, 11, 2] 答案: ...
- Pycharm节能模式
如题,开启节能模式代码不会自动补全.
- SRM 558 SurroundingGame
题意: 给定一个网格,每个网格有选取代价和占据收益.每个点被占据,需要满足以下两个条件至少一个条件:1.被选取 2.邻近方格都被选取(有公共边被称为邻近) 不一定要占据所有方格,求最大收益. 输入 ...
- POJ 3590 The shuffle Problem
Any case of shuffling of n cards can be described with a permutation of 1 to n. Thus there are total ...
- [HNOI2015]接水果
题目描述 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果.由于她已经DT FC 了The big black, 她觉得这个游戏太简单了,于是发明了一个更加难的版本. 首先有 ...
- Git常用命令及常见问题解决
$ mkdir xxx ----创建xxx目录 $ cd learngit ----切到xxx目录下 $ pwd ----查看当前文件所在目录 $ gi ...
- JavaScript中的类
JavaScript类的相关知识 1.例子 /* 例1 */// 定义一个构造函数function Range(from, to){ this.from = from; this.to = ...
- C语言 递归 汉诺塔问题 最大公约数问题
函数不能嵌套定义,但能嵌套调用(在调用一个函数的过程中再调用另一个函数) 函数间接或直接调用自己,称为递归调用 汉诺塔问题 思想:简化为较为简单的问题 n=2 较为复杂的问题,采用数学归纳方法分析 ...