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);
//清除工作
}
#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);
//清除工作
}
相关文章推荐
- IIS6.0日志文件分析代码_1生成访问字段记录到数组中
- adodb.stream读文件到数组的代码
- ios NSArray数组写plist的文件的代码
- 如何在winhex里面将一个文件转成C语言数组存储
- C语言中执行python代码或源程序文件(高级嵌入方式)
- C语言中文件操作--如何将文本数据正确读到字符串数组中
- C语言读取文件方面 代码的优化
- php中将数组存到文件里的实现代码
- c语言细节中的文件归类代码
- C语言文件读写处理通用代码
- 一个hex文件与bin文件相互转换的python代码
- C Array Dumper(一个将任意文件转成C语言数组文本的东西)
- 24bit BMP转C数组及BIN文件的程序(By wwfiney@ARMCE)
- PHP读取txt文件的内容并赋值给数组的代码
- 让UltraEdit自动对pro*c代码文件语法着色为c语言格式
- IIS6.0日志文件分析代码_1生成访问字段记录到数组中
- 文件操作简易代码[C语言]
- 栈数组实现头文件C语言
- U-boot移植步骤详解_附:bin文件和所需文件(代码+流程图+遇到的错误及解决办法)
- 无BOM的条件下C语言判断一个文件是否为UTF-8编码代码(windows,linux通用)