如何将一个二进制的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文件来进 ...
随机推荐
- sqlserver数据库导入Mysql数据库问题
近来遇到一个问题,之前的项目用的是SQLServer数据库,但是现在要换成MySQL数据库,所有整理了一些数据导入的步骤,供需要的人参考! 第一步: 第二步: 第三步: 第四步: 第五步: 第六步: ...
- [LeetCode] Toeplitz Matrix 托普利兹矩阵
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...
- Python模块 - configparser
configparser模块用来对配置文件进行操作 1.获取所有块 import configparser config = configparser.ConfigParser() config.re ...
- NC的开发模型
2018-04-1622:35:12 NC的开发模型 系统前端为:客户UI代码,UI代码继承ToftPanel,UI代码通过调用远程组件和服务端进行交互,中间传递的数据模型为VO,远程组件调用底层的业 ...
- [HNOI 2017]礼物
Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一个送给她.每个手环上各有 n 个装饰物,并且每个装饰物都有一定的亮度.但是在 ...
- HDU 2082 找单词
Problem Description 假 设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字 ...
- bzoj 1272: [BeiJingWc2008]Gate Of Babylon
Description Solution 如果没有限制,答案就是 \(\sum_{i=0}^{m}C(n+i-1,i)\) 表示枚举每一次取的个数,且不超过 \(m\),方案数为可重组合 发现这个东西 ...
- 2015 多校联赛 ——HDU5344(水)
Problem Description MZL loves xor very much.Now he gets an array A.The length of A is n.He wants to ...
- web框架和django基础(粗糙版)
web框架本质: 浏览器:socket客户端 服务器:socket服务端 1.自己写socket服务端(最傻) #!/usr/bin/env python ...
- salesforce lightning零基础学习(二) lightning 知识简单介绍----lightning事件驱动模型
看此篇博客前或者后,看一下trailhead可以加深印象以及理解的更好:https://trailhead.salesforce.com/modules/lex_dev_lc_basics 做过cla ...