C語言將xxx.bin文件轉(zhuǎn)為數(shù)組
剛開始工作的時候?qū)崿F(xiàn)過公司的一個項(xiàng)目需求,大致描述如下:
實(shí)現(xiàn)一個SPI-flash讀寫程序,目的是要將一個二進(jìn)制文件寫到SPI_FLASH中,最后通過開機(jī)讀取,實(shí)際上這個.bin文件就是uboot和second-boot的結(jié)合體。通過SD卡寫到SPI-FLASH中就可以脫離SD卡開機(jī)啟動了,我也不知道領(lǐng)導(dǎo)為什么要我寫這個東西,直接把bin通過一個小工具寫進(jìn)去不就可以了嗎?不管那么多,咱按需求做就行了。這類型的工具百度一搜也一大堆,比我當(dāng)時做的考慮的更全面,我當(dāng)時只是為了趕緊把東西搞出來,所以就沒考慮太多,畢竟這東西就用一次。
項(xiàng)目最終的功能如下:
/*
Date:2016.12.16
author:楊源鑫
*/
按照提示輸入:
當(dāng)前目錄下的bin文件的文件名
ep : xxx.bin
接著輸入:
要生成的.h文件的名稱:
ep : xxx.h
會在目錄下自動生成.h文件:
.h文件內(nèi)包括兩個數(shù)組
一個名稱是SPIflashimage,這個就是從.bin文件中讀出來的數(shù)組。
另一個是預(yù)留的空數(shù)組mfgimage,這個可以作為清數(shù)組的時候用,當(dāng)然可以自由去改大小。
程序?qū)崿F(xiàn)如下:
/*
將二進(jìn)制轉(zhuǎn)化成數(shù)組頭文件
*/
//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);
//創(chuàng)建一個文件用于保存零數(shù)組
fd = creat("nufile.bin", 0777);
if(-1 == fd)
{
perror("creat fair!");
return ;
}
//偏移寫空
int offset = lseek(fd, 4096 * 2, SEEK_END);
write(fd, "", 1);
/**************************************************/
//清數(shù)組
for(i = 0 ; i < 10 ; i++)
pbuf[i] = 0 ;
for(i = 0 ; i < 4096 * 2 ; i++)
mfgimage[i] = 0 ;
//寫第二個數(shù)組
fwrite(array1, strlen(array1), 1, infile);
//從空文件里讀數(shù)據(jù)讀到mfgimage數(shù)組
read(fd, mfgimage, 4096 * 2);
//關(guān)閉文件句柄
close(fd);
//往文件后面繼續(xù)寫數(shù)據(jù)
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);
//刪除當(dāng)前目錄下的一個空洞文件
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};
//讀取目標(biāo).bin文件
printf("please input src file path\n");
scanf("%s", srcbin);
//創(chuàng)建一個.h頭文件用于保存bin轉(zhuǎn)C數(shù)組的文件
printf("please input output path\n");
scanf("%s", dstfile);
//獲取文件的大小
size = GetBinSize(srcbin);
//申請用于存放該文件的數(shù)組
buf = (unsigned char *)malloc(sizeof(unsigned char) * size);
//讀取文件
read_bin(srcbin, buf, size);
//制作頭文件,該頭文件下含有兩個數(shù)組,一個是有數(shù)據(jù)的,另外一個是全0數(shù)組
//全0主要備用,以后要清空可以調(diào)用這個數(shù)組
OutPut_handle(dstfile, buf, size);
return 0;
}
運(yùn)行結(jié)果:
生成的.h內(nèi)容如下,太多了,我只截取一部分出來:
#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
往期精彩
開源STM32產(chǎn)品:無線點(diǎn)菜寶使用評測
【Linux系統(tǒng)編程】可重入和不可重入函數(shù)
C語言表驅(qū)動法編程實(shí)踐(精華帖,建議收藏并實(shí)踐)
分享一個在Keil開發(fā)環(huán)境中配置代碼格式化工具Astyle(美化代碼風(fēng)格)
STM32CubeMX + STM32F1系列開發(fā)時遇到的四個問題及解決方案分享
若覺得本次分享的文章對您有幫助,隨手點(diǎn)[在看]
并轉(zhuǎn)發(fā)分享,也是對我的支持。
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務(wù)。文章僅代表作者個人觀點(diǎn),不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!