-
目的:
GIF文件转为头文件
-
举例:
用UE打开GIF文件,如下图所示:
图1 test.gif文件
将上面文件内容转化为头文件,放到一个数组里面,内容如下:
图2 test.h文件 -
思路:
从上面可知,将二进制文件转换为文本文件,十六进制 47 转为 0x47,其他类推。
-
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> static char pre_compiler_str_begin[] = "#ifdef PIP_PNG_GIF";
static char pre_compiler_str_end[] = "#endif";
static char const_char[] = "const char ";
static char sign_begin[] = "[] = {";
static char sign_end[] = "};"; int match_file(unsigned char *buf, int file_size, char *src_file_name)
{
unsigned char *p_start = NULL, *p_end = NULL;
char *head_data = NULL;
char str[64], filename[256], *p;
int i,ret = 0; if((buf == NULL) || (src_file_name==NULL))
{
return -1;
} p_end = buf; if ((strlen(src_file_name) > 4)
&& (strcmp(src_file_name + strlen(src_file_name) - 4, ".gif") == 0))
{
strncpy(filename, src_file_name, strlen(src_file_name) - 4);
strcpy(filename + strlen(src_file_name) - 4, ".h");
}
else
{
sprintf(filename, "%s.h", src_file_name);
} do
{
FILE *head_file = NULL;
head_file = fopen(filename, "w");
if (head_file == NULL)
{
ret = -1;
fprintf(stderr,"[%s] Open file %s error!\n", src_file_name, filename);
break;
} // write str:
// #ifdef PIP_PNG_GIF
// const char test0_gif[] = {
fwrite(pre_compiler_str_begin, 1, strlen(pre_compiler_str_begin), head_file);
fputc('\n',head_file); fwrite(const_char, 1, strlen(const_char), head_file);
p = strrchr(filename, '\\')+1;
fwrite(p, 1, strlen(p)-2, head_file);
fwrite(sign_begin, 1, strlen(sign_begin), head_file);
fputc('\n',head_file); // write data
for(i = 0; i < file_size; i++)
{
memset(str, 0, sizeof(str));
sprintf(str, "%s%0.2x%c", "0x", buf[i], ',');
fwrite(str, 1, strlen(str), head_file);
if((i == file_size - 1) || (i % 16 == 15))
{
fputc('\n',head_file);
}
} fwrite(sign_end, 1, strlen(sign_end), head_file);
fputc('\n',head_file); fwrite(pre_compiler_str_end, 1, strlen(pre_compiler_str_end), head_file);
fputc('\n',head_file); fclose(head_file);
} while (0); return ret;
} int main(int argc, char *argv[])
{
FILE *input = NULL;
char *file_name = NULL;//"test.h";
unsigned char *bmp_data = NULL;
int i;
int file_size = 0; if (argc <= 1)
{
printf("Please assign input files!\n");
return -1;
} for (i=1; i<argc; i++)
{
file_name = argv[i]; input = fopen(file_name, "rb");
if (!input)
{
fprintf(stderr, "[%s] Cannot open file!\n",file_name);
continue;
}
fseek(input, 0, SEEK_END);
file_size = ftell(input);
bmp_data = (unsigned char *)malloc(file_size + 1);
fseek(input, 0, SEEK_SET);
fread(bmp_data, 1, file_size, input);
bmp_data[file_size] = '0'; if(match_file(bmp_data, file_size, file_name) == 0)
{
fprintf(stdout, "[%s]\tOK\n",file_name);
}
else
{
fprintf(stdout, "[%s] Failed!\n",file_name);
}
} return 0; }
相关文章
- LoadRunner接口测试(三)-LoadRunner11+破解文件+汉化文件+删除注册表工具(转)
- chrome 书签 文件夹_将Chrome书签工具栏文件夹转换为图标
- 如何生成转储(dmp)文件--工具篇
- Easy CHM(chm制作工具) 3.93 破解安装版 (将word文件转chm文件)
- 如何能把文本转换为GIF文件?
- 用 Delphi 7 实现基于 FFMS2 的视频转 GIF 工具 [原创] - 呆呆大虾
- kgtemp文件转mp3工具
- [转]数据恢复 文件恢复工具 DiskGenius v4.9.1 绿色专业版及单文件
- 开源小工具 酷狗、网易音乐缓存文件转mp3工具
- gcc编译时头文件库文件搜索顺序(转)