#include <stdio.h>
/*
 * Copyright (C) 2002 Adam Bernstein, all rights reserved.
 *
 * Convert file containing ASCII hexidecimal numbers representing
 * binary data to binary values.  Read each line of ASCII data,
 * and walk through each 2 characters, converting them to hex, then
 * write that output to stdout.
 */
int main(int argc, char *argv[])
{
    FILE *fp;
    char *file;
    char *cp;
    char buf[256];
    char hexstr[3];
    char hexval;

    if (argc == 1) {
        printf("usage: %s ascii_data_file\n", argv[0]);
        printf("       output binary data is written to stdout\n");
        return 1;
    }
    file = argv[1];

    fp = fopen(file, "r");
    if (!fp) {
        perror("fopen");
        return 1;
    }

    cp = fgets(buf, sizeof(buf)-1, fp);
    while (cp && !feof(fp)) {
        cp = buf + strlen(buf) - 1;
        if (*cp == '\n' || *cp == '\r') *cp-- = '\0';
        if (*cp == '\n' || *cp == '\r') *cp-- = '\0';
        cp = buf;
        while (*cp) {
            hexstr[0] = *cp++;
            hexstr[1] = *cp++;
            hexstr[2] = '\0';
            hexval = strtol(hexstr, 0, 16);
            fwrite(&hexval, 1, 1, stdout);
        }
        cp = fgets(buf, sizeof(buf)-1, fp);
    }
    return 0;
}
