源代码:

#include <stdio.h>
#include <stdlib.h>

#define SUCCESS 0
#define FAILURE 1

#define IN
#define OUT
#define INOUT

typedef unsigned long BOOL_T;
typedef unsigned char UCHAR;
typedef unsigned long ULONG;

#define BOOL_TRUE (BOOL_T)1
#define BOOL_FALSE (BOOL_T)0

static inline FILE* fopen_s(const char *fname, const char *mode)
{
    FILE *pfRet = NULL;

    if (NULL == fname || NULL == mode)
    {
        fprintf(stderr, "line:%d,wrong param [fname or mode = NULL]\n", __LINE__);
        return NULL;
    }
    pfRet = fopen(fname, mode);
    if (NULL == pfRet)
    {
        fprintf(stderr, "line:%d,fopen() failed", __LINE__);
        return NULL;
    }

    return pfRet;
}

ULONG hex2Dec(IN UCHAR aucHex[], OUT UCHAR *pucDec)
{
    ULONG i = 0;
    ULONG weight = 0;
    BOOL_T bflag = BOOL_TRUE;

    if (NULL == pucDec)
    {
        fprintf(stderr, "line:%d,wrong param [pucDec = NULL]\n", __LINE__);
        return FAILURE;
    }

    *pucDec = 0;
    for (i = 0; i < 2; ++i)
    {
        weight = bflag ? 16 : 1;
        if ('a' <= aucHex[i] && 'f' >= aucHex[i])
        {
            *pucDec += (UCHAR)((aucHex[i] - 'a' + 10) * weight);
        }
        else if ('0' <= aucHex[i] && '9' >= aucHex[i])
        {
            *pucDec += (UCHAR)((aucHex[i] - '0') * weight);
        }
        else
        {
            return FAILURE;
        }

        bflag = BOOL_FALSE;
    }

    return SUCCESS;
}

ULONG removeSpInFile(const char *pcFilePath)
{
    FILE *pfOriginal = NULL;
    FILE *pfTmp = NULL;
    int ich = 0;
    int iRet = 0;

    if (NULL == pcFilePath)
    {
        fprintf(stderr, "line:%d,wrong param [pcFilePath = NULL]\n", __LINE__);
        return FAILURE;
    }

    pfOriginal = fopen_s(pcFilePath, "r");

    //创建临时文件
    pfTmp = tmpfile();
    if (NULL == pfTmp)
    {
        fprintf(stderr, "line:%d,tmpfile() failed\n", __LINE__);
        fclose(pfOriginal);
        return FAILURE;
    }

    while (EOF != (ich = fgetc(pfOriginal)))
    {
        if (' ' == ich || '\n' == ich || '\r' == ich)
        {
            continue;
        }

        fputc(ich, pfTmp);
    }

    fclose(pfOriginal);
    iRet = remove(pcFilePath);
    if (SUCCESS != iRet)
    {
        fprintf(stderr, "line:%d,remove() failed\n", __LINE__);
        fclose(pfTmp);
        return FAILURE;
    }

    pfOriginal = fopen_s(pcFilePath, "w");
    rewind(pfTmp);

    while (EOF != (ich = fgetc(pfTmp)))
    {
        fputc(ich, pfOriginal);
    }

    fclose(pfOriginal);
    fclose(pfTmp);

    return SUCCESS;
}

ULONG txt2bin(const char *pcFilePath)
{
    FILE *pfOriginal = NULL;
    FILE *pfTmp = NULL;
    ULONG ulRet = SUCCESS;
    UCHAR aucHex[2] = {0};
    UCHAR ucDec = 0;
    int ich = 0;

    ulRet = removeSpInFile(pcFilePath);
    if (SUCCESS != ulRet)
    {
        fprintf(stderr, "line:%d,removeSpInFile() failed\n", __LINE__);
        return FAILURE;
    }

    pfOriginal = fopen_s(pcFilePath, "rb");

    //创建临时文件
    pfTmp = tmpfile();
    if (NULL == pfTmp)
    {
        fprintf(stderr, "line:%d,tmpfile() failed\n", __LINE__);
        fclose(pfOriginal);
        return FAILURE;
    }

    while (EOF != fscanf(pfOriginal, "%c%c", &aucHex[0], &aucHex[1]))
    {
        ulRet = hex2Dec(aucHex, &ucDec);
        if (SUCCESS != ulRet)
        {
            fprintf(stderr, "line:%d,hexToDec() failed\n", __LINE__);
            return FAILURE;
        }
        fputc(ucDec, pfTmp);
    }

    fclose(pfOriginal);
    ulRet = (ULONG)remove(pcFilePath);
    if (SUCCESS != ulRet)
    {
        fprintf(stderr, "line:%d,remove() failed\n", __LINE__);
        fclose(pfTmp);
        return FAILURE;
    }

    pfOriginal = fopen_s(pcFilePath, "wb");
    rewind(pfTmp);

    while (EOF != (ich = fgetc(pfTmp)))
    {
        fputc(ich, pfOriginal);
    }

    fclose(pfOriginal);
    fclose(pfTmp);

    return SUCCESS;
}

int main(int argc, char *argv[])
{
    ULONG ulRet = 0;

    if (2 != argc)
    {
        fprintf(stderr, "Usage: %s <fname>\n", argv[0]);
        return FAILURE;
    }

    ulRet = txt2bin(argv[1]);
    if (SUCCESS != ulRet)
    {
        fprintf(stderr, "line:%d,txt2bin() failed\n", __LINE__);
        return FAILURE;
    }

    return SUCCESS;
}