• 目的:

    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. SQL SERVER 查询死锁

    USE mastergo CREATE PROCEDURE [dbo].[sp_who_lock]AS     BEGIN        DECLARE @spid INT ,             ...

  2. NSLayoutConstraint-代码实现自己主动布局的函数使用方法说明

    [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelat ...

  3. WPF combobox 圆角制作

    修改ComboBox的Template, 在VS 2010或者Blend中你可以导出ComboBox的默认模板: VS2010中: 然后修改里面的模板,比如: <Window x:Class=& ...

  4. MVC-@html.ActionLink的几种参数格式

    一 Html.ActionLink("linkText","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法, ...

  5. 如何学会web前端开发

    如何学会web前端开发 http://jingyan.baidu.com/article/b7001fe17623970e7282dd0c.html http://www.yangqq.com/dow ...

  6. [Webpack 2] Intro to the Production Webpack Course

    There are several lessons that will build on top of this project. It is a fairly standard, small web ...

  7. JSP 内置对象的四种属性范围

    在jsp页面中的对象,包括用户创建的对象(例如,javaBean对象)和JSP的隐含对象,都有一个范围属性.范围定义了在什么时间内,在哪一个JSP页面中可以访问这些对象.例如,session对象在会话 ...

  8. mybatis0201 01复习

    mybatis是什么? mybatis是一个持久层框架,是apache下的开源项目,前身是itbatis,是一个不完全的ORM框架(因为mybatis提供输入和输出的映射,需要程序员自己写sql语句) ...

  9. iOS UIKit:CollectionView之布局(2)

    Collection view使用UICollectionViewFlowLayout对象来管理section中的cell,该对象是一种流布局方式,即在collection view中的section ...

  10. MSDN Webcast 系列课程

    云计算(Cloud) 云起龙骧系列课程 网络开发(Web) ASP.NET 4 风云之旅系列课程 ASP.NET 开发实践系列课程 MOSS2007 最佳实战 StepByStep 系列课程 Silv ...