• 目的:

    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; }

GIF文件转换为头文件工具的更多相关文章

  1. APNS .p12文件转换为 .pem文件

    1:先用mac的钥匙串工具,把APN的推送证书转换为 .p12文件: 2:在mac的终端下 把.p12文件转换为 .pem文件 openssl pkcs12 -in apns-dev-cert.p12 ...

  2. 自定义C/C++头文件以及头文件重复定义解决

    今天再看二叉树的知识,看着看着就看到C/C++的头文件及头文件重复定义这一块去了.以前就看到过这个问题,但是自己一直没有用到这方面的东西,今天遇到就顺便总结一下,等以后忘了再回来看看. 首先明确一点C ...

  3. C语言常用的库文件(头文件、函数库)

    C语言常用的库文件(头文件.函数库) C系统提供了丰富的系统文件,称为库文件.C的库文件分为两类,一类是扩展名为".h"的文件,称为头文件,在前面的包含命令中我们已多次使用过.在& ...

  4. 使用VirtualBox把IMG文件转换为VDI文件

    使用VirtualBox把IMG文件转换为VDI文件 首先确保已安装VirtualBox. 需要使用的命令: 语法:$ VBoxManage convertdd input.img output.vd ...

  5. python之模块py_compile用法(将py文件转换为pyc文件)

    # -*- coding: cp936 -*- #python 27 #xiaodeng #python之模块py_compile用法(将py文件转换为pyc文件):二进制文件,是由py文件经过编译后 ...

  6. Android.mk文件c++头文件包含问题

    Eclipse 中 Android.mk文件c++头文件包含问题 jni中的目录结构如下: 编译找不到头文件 LOCAL_PATH := $(call my-dir)LOCAL_C_INCLUDES ...

  7. 永远也记不住的linux环境变量,库文件,头文件,交叉编译...

    一.环境变量1.node-v4.9.1-linux-armv7l1)安装cp node-v4.9.1-linux-armv7l.tar.gz /usr/local/cd /usr/local/tar ...

  8. Python如何实现doc文件转换为docx文件?

    Python如何实现doc文件转换为docx文件? 在开发过程中遇到一个关于读写doc和docx的问题: 一个文件夹中有两种文件, 一种为doc结尾, 一种为docx结尾, 需要将这些文件全部重命名. ...

  9. TensorFlow的checkpoint文件转换为pb文件

    由于项目需要,需要将TensorFlow保存的模型从ckpt文件转换为pb文件. import os from tensorflow.python import pywrap_tensorflow f ...

随机推荐

  1. Jquery- 错误消息"Date"未定义,"String"未定义

    在jquery的高版本(1.7-1.9)提示“String”未定义,稍低版本提示“Date”未定义错误 解决办法: 1.找到你发生错误的代码(即执行那条代码时发生的错误) 2.使用setTimeout ...

  2. java 递归函数

    一.递归函数,通俗的说就是函数本身自己调用自己...  如:n!=n(n-1)!  你定义函数f(n)=nf(n-1)  而f(n-1)又是这个定义的函数..这就是递归  二.为什么要用递归:递归的目 ...

  3. 在Windows下使用MinGW静态编译Assimp

    使用MinGW静态编译Assimp 到了5月份了,没有写一篇日志,于是自己从知识库里面拿出一篇文章充数吧.这次将要解说怎样在Windows下使用MinGW静态编译Assimp. Assimp是眼下比較 ...

  4. 即时通讯(IM)

    即时通讯(IM)功能是APP的重要功能之一,而开发好移动IM却绝非易事.通常来说,IM技术选型至少要解决以下问题:1. 协议选型 2. IM服务器选型 3. 对协议和服务器做相应修改,通常来说直接拿个 ...

  5. [React] React Fundamentals: Precompile JSX

    The JSX Transformer library is not recommended for production use. Instead, you'll probably want to ...

  6. [React] React Fundamentals: Build a JSX Live Compiler

    we want to have the ability to write JSX and see the output live in the browser. <!doctype html&g ...

  7. thinkphp连接oracle

    配置文件中: //Oracle 测试环境    'DB_TYPE'     => 'Oracle',             // 数据库类型    'DB_HOST'     => '1 ...

  8. Java基础知识强化之网络编程笔记09:TCP之客户端键盘录入服务器写到文本文件中

    1. TCP之客户端键盘录入服务器写到文本文件中 (1)客户端: package cn.itcast_09; import java.io.BufferedReader; import java.io ...

  9. CSS hack常用方案(摘选)

    邮箱因为默认了line-height?:170%,导致采用table元素时继承问题,可以采用line-height:50% 很好解决. 常 在使用float时,后面的显示不正常,因为继承了float了 ...

  10. Windows7服务无法启动的解决

    这周六,我接到了一个很诡异的案例,表现为任务栏右下角网络连接图标始终为一个红叉,已排除网卡硬件.链路和网卡驱动的问题.主板都新换了一块,可是问题依旧,这无疑将问题的根源指向了操作系统.本想通过网络疑难 ...