GIF文件轉爲頭文件html
用UE打開GIF文件,以下圖所示:
圖1 test.gif文件
將上面文件內容轉化爲頭文件,放到一個數組裏面,內容以下:
圖2 test.h文件數組
從上面可知,將二進制文件轉換爲文本文件,十六進制 47 轉爲 0x47,其餘類推。3d
#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; }