如何在winhex里面将一个文件转成C语言数组存储
1个回答
展开全部
你不如自己写一个bin2c的代码,然后编译运行。
给你一个做参考。
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "g_def.h"
#define OUT_BYTES_PER_LINE 16
#define MAX_ARRAY_NAME_LEN 20
typedef struct bin2c
{
char *in_name,
*out_name,
*array_name;
BOOL out_alloc;
char *out_buffer;
int out_buffer_size;
FILE *in_fp,
*out_fp;
int file_size;
}BIN2C_S, *BIN2C;
static inline void print_usage(void)
{
printf("Usage @gtool: bin2c.x in_file out_file [array_name]\n");
printf("convert bin file to c array\n");
printf("Arguments:\n");
printf("\tin_file:\tthe file name of input file\n");
printf("\tout_file:\toutput filename\n");
printf("\tarray_name:\toptional. if no array name set, will only convert data but not set array\n");
}
static inline BIN2C destroy_bin2c(BIN2C inst)
{
if(inst == NULL) return inst;
if(inst->out_alloc && inst->out_name)
free(inst->out_name);
if(inst->out_buffer)
free(inst->out_buffer);
if(inst->in_fp)
fclose(inst->in_fp);
if(inst->out_fp)
fclose(inst->out_fp);
free(inst);
return NULL;
}
static void push_header(BIN2C inst)
{
char array_def[MAX_ARRAY_NAME_LEN + 10] = {0};
char buffer[100];
int i;
if(inst->array_name == NULL) return;
strncpy(array_def, inst->array_name, MAX_ARRAY_NAME_LEN);
for(i = 0; i < strlen(array_def); i ++)
if(array_def[i] <= 'z' && array_def[i] >= 'a') array_def[i] += 'A' - 'a';
strcat(array_def, "_LEN");
sprintf(buffer, "#define %s %d\n\n", array_def, inst->file_size);
fputs(buffer, inst->out_fp);
sprintf(buffer, "const unsigned char %s[%s] = \n{\n", inst->array_name, array_def);
fputs(buffer, inst->out_fp);
}
static void push_tail(BIN2C inst)
{
if(inst->array_name == NULL) return;
fputs("};\n\n", inst->out_fp);
}
static void convert(BIN2C inst)
{
int left_size, read_size, read;
int i;
U8 buffer[OUT_BYTES_PER_LINE];
BOOL not_last_line;
left_size = inst->file_size;
if(inst->array_name)
push_header(inst);
while(left_size > 0)
{
memset(buffer, 0, OUT_BYTES_PER_LINE);
memset(inst->out_buffer, 0, inst->out_buffer_size);
read_size = left_size > OUT_BYTES_PER_LINE ? OUT_BYTES_PER_LINE : left_size;
not_last_line = left_size > OUT_BYTES_PER_LINE;
read = fread(buffer, 1, read_size, inst->in_fp);
if(read != read_size)
printf("want to read %d bytes, but read %d bytes only\n", read_size, read);
for(i = 0; i < read; i ++)
{
sprintf(&inst->out_buffer[strlen(inst->out_buffer)], "0x%02X", buffer[i]);
if(i != read - 1)
strcat(inst->out_buffer, ", ");
else if(not_last_line)
strcat(inst->out_buffer, ",");
}
fputs(inst->out_buffer, inst->out_fp);
fputs("\n", inst->out_fp);
left_size -= read;
}
if(inst->array_name)
push_tail(inst);
}
int main(int argc, char *argv[])
{
#define OUT(x) do {inst = destroy_bin2c(inst); if(x != 0)print_usage(); return x;} while(0)
BIN2C inst = NULL;
int tmp;
if(argc < 2) OUT(-1);
inst = malloc(sizeof(*inst));
if(inst == NULL) OUT(-2);
memset(inst, 0, sizeof(*inst));
inst->in_name = argv[1];
if(argc >= 3) inst->out_name = argv[2];
else
{
tmp = strlen(inst->in_name);
tmp += 5;
inst->out_name = malloc(tmp);
if(inst->out_name == NULL) OUT(-3);
inst->out_alloc = TRUE;
strcpy(inst->out_name, inst->in_name);
strcat(inst->out_name, ".dat");
}
if(argc >= 4)
{
inst->array_name = argv[3];
tmp = strlen(inst->array_name);
if(tmp > MAX_ARRAY_NAME_LEN)
{
printf("too long array name set, use first %d bytes\n", MAX_ARRAY_NAME_LEN);
inst->array_name[MAX_ARRAY_NAME_LEN] = 0;
}
tmp = inst->array_name[0];
if(!(tmp == '_' || (tmp <= 'z' && tmp >= 'a') || (tmp <= 'Z' && tmp >= 'Z')))
inst->array_name[0] = '_';
for(tmp = 1; tmp < strlen(inst->array_name); tmp ++)
if(!(inst->array_name[tmp] == '_' || (inst->array_name[tmp] <= 'z' && inst->array_name[tmp] >= 'a') || (inst->array_name[tmp] <= 'Z' && inst->array_name[tmp] >= 'Z') || (inst->array_name[tmp] <= '9' && inst->array_name[tmp] >= '0')))
inst->array_name[tmp] = '_';
printf("using array name %s\n", inst->array_name);
}
if(argc > 4)
{
printf("warning: too many parameters input. the max input parameters number is 3\n");
printf("tool can work, but other parameter(s) will be ignored\n");
print_usage();
}
inst->in_fp = fopen(inst->in_name, "rb");
if(inst->in_fp == NULL)
{
printf("error: can not open file %s to read\n", inst->in_name);
OUT(-4);
}
fseek(inst->in_fp, 0, SEEK_END);
inst->file_size = ftell(inst->in_fp);
printf("get file size is %d\n", inst->file_size);
fseek(inst->in_fp, 0, SEEK_SET);
if(inst->file_size == 0)
{
printf("error: no data in %s\n", inst->in_name);
OUT(-7);
}
inst->out_fp = fopen(inst->out_name, "wb");
if(inst->out_fp == NULL)
{
printf("error: can not open file %s to write\n", inst->out_name);
OUT(-5);
}
inst->out_buffer_size = OUT_BYTES_PER_LINE * 10;
inst->out_buffer = malloc(inst->out_buffer_size);
if(inst->out_buffer == NULL) OUT(-6);
convert(inst);
OUT (0);
}
当然可以写的比这更简单一点。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询