您的位置:首页 > 编程语言 > C语言/C++

bin文件转成C语言数组之c代码

2014-12-11 16:12 155 查看
反汇编的时候用的着。

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#include "string.h"

#include <malloc.h>

#define i2a(j) if( j>=0 && j<=9 ){ \

j=j+'0'; \

}else{ \

j = j-0x0A+'A'; \

}

int main( void )

{

int len;

int c,j;

char line[1024];

FILE *fp;

FILE *fp_temp;

fp = fopen("stm32_aadr_0.bin", "rb");

//以二进制的方式打开bin文件

if( fp == NULL ){

printf("1.file is not exist.\n" );

return -1;

}

memset(line,0x00,1024);

j=0x00;

fp_temp = fopen("test.txt", "w+");

if( fp_temp == NULL ){

printf("2.file is not exist.\n" );

return -1;

}

//seek to the beginning of the file

fseek(fp, 0, SEEK_SET);

fseek(fp_temp, 0, SEEK_SET);

len = 0;

while(1){

c = fgetc(fp);

if( feof(fp) ){

//check for EOF

break;

}

//printf("c = %x.\n", c );

line[0]='0';

line[1]='x';

j = (c&0xf0) >> 4;

i2a(j);

line[2] = j;

j = (c&0x0f) >> 0;

i2a(j);

line[3] = j;

line[4]=',';

line[5]=' ';

++len;

if( len%16 == 0x00 ){

line[6] = '\r';

line[7] = '\n';

fwrite( &line[0], 8, 1, fp_temp );

}else{

fwrite( &line[0], 6, 1, fp_temp );

}

}

fclose(fp);

fclose(fp_temp);

//清除工作

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: