概述
把一个二进制文件转换为c语言数组:
#include "stdafx.h"
#include
#include
#include
#include
#include
#include "string.h"
unsigned int GetFileSize(char *filename);
void read_src(char *path, unsigned char *buf, unsigned int size);
void make_dstfile(char *outpath, unsigned char *buf, unsigned int size);
int main()
{
unsigned char *buf = NULL;
unsigned int size;
char srcbmp[100]={0};
char dstfile[100]={0};
//char srcbmp[100]="test.bin";
//char dstfile[100]="test.h";
printf("Please input src file namenC file path namen");
scanf("%s %s",srcbmp,dstfile);
size = GetFileSize(srcbmp);
buf = (unsigned char *)malloc(sizeof(unsigned char)*size);
read_src(srcbmp, buf, size);
make_dstfile(dstfile, buf, size);
return 0;
}
unsigned int GetFileSize(char *filename)
{
unsigned int siz = 0;
FILE *fp = fopen(filename, "rb");
if (fp)
{
fseek(fp, 0, SEEK_END);
siz = ftell(fp);
fclose(fp);
}
return siz;
}
void read_src(char *path, unsigned char *buf, unsigned int size)
{
FILE *infile;
if((infile=fopen(path,"rb"))==NULL)
{
printf( "nCan not open the path: %s n", path);
exit(-1);
}
fread(buf, sizeof(unsigned char), size, infile);//printf("n打开的图为 %d",img->bfType);
fclose(infile);
}
void make_dstfile(char *outpath, unsigned char *buf, unsigned int size)
{
FILE *infile;
int i,j,k,n;
char pbuf[10]={0};
if((infile=fopen(outpath,"wa+"))==NULL)
{
printf( "nCan not open the path: %s n", outpath);
exit(-1);
}
k=0;
fwrite("u8 temp_array[] = {n",strlen("u8 m_firmware_data_200[] = {n"),1,infile);
for(i = 0; i < size; i++)
{
k++;
sprintf(pbuf,"0x%02x",buf[i]);
fwrite(pbuf,strlen(pbuf),1,infile);
if(k != 8)
fwrite(", ",strlen(", "),1,infile);
else
fwrite(",",strlen(","),1,infile);
if(k==8)
{
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);
fclose(infile);
}
最后
以上就是无情便当为你收集整理的c语言 转换成二进制 数组,二进制文件转化成数组的全部内容,希望文章能够帮你解决c语言 转换成二进制 数组,二进制文件转化成数组所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复