1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <stdio.h>
#include <time.h>
#include <string.h>
static int in2hex(char *ptr)
{
char str[3];
unsigned int val;
memcpy(str, ptr, 2);
str[2] = '\0';
sscanf(str, "%x", &val);
return val;
}
static unsigned char swap(unsigned char c)
{
unsigned char r = 0;
int i;
for (i = 0 ; i < 8 ; i++)
{
r <<= 1;
if (c & 1)
r |= 1;
c >>= 1;
}
return r;
}
int main(int ac, char *av[])
{
int nb, type, i;
int first = 1;
time_t temps;
FILE *fptr;
char buf[256];
if (ac != 3)
{
fprintf(stderr, "format : mcs2h 1200|9600 filename\n");
return 1;
}
fptr = fopen(av[2], "r");
if (fptr == NULL)
{
fprintf(stderr, "file %s not found\n", av[2]);
return 1;
}
time(&temps);
printf( "/*\n"
" *\n"
" * File %s converted to h format by mcs2h\n"
" *\n"
" * (C) F6FBB 1998\n"
" *\n"
" * %s"
" *\n"
" */\n\n",
av[2], ctime(&temps));
printf("static unsigned char bits_%s[]= {\n", av[1]);
while (fgets(buf, sizeof(buf), fptr))
{
nb = in2hex(buf+1);
type = in2hex(buf+7);
if (type != 0)
continue;
if (first)
first = 0;
else
printf(",\n");
for (i = 0 ; i < nb ; i++)
{
printf("0x%02x%s", swap(in2hex(buf+9+i*2)), (i < (nb-1)) ? "," : "");
}
}
printf(" };\n");
fclose(fptr);
return 0;
}
|