#include #include #include #include char *buffer; char * testchar(char *in){ char *nN; nN = (char*) malloc(256*sizeof(char)); return (char*)nN; } /* Generate the output file name based on in */ char * OPFileName (char *in){ int index; char *nN; nN = (char*) malloc(256*sizeof(char)); index=0; while (in[index]!='\0' && in[index]!='.'){ nN[index]=in[index]; index++; } nN[index]='\0'; strcat(nN,".b\0"); return (char*)nN; } /* Reads in 1 hex digit, skips all other characters, replaces eof with \0*/ char readInput(FILE *fptr){ char c; fscanf(fptr,"%c",&c); while (!feof(fptr)){ if (isxdigit(c)) return c; fscanf(fptr,"%c",&c); } return '\0'; } /* Reads the text file looking for hex characters, once it reads byte width chars it converts the string to an int. and returns the result*/ int buildInt(FILE *fptr, int size){ char str[9]="\0"; int loop=0; int num; unsigned char c; for(loop=0;loop<2*size;loop++){ c=readInput(fptr); if (feof(fptr)) break; str[loop]=c; } num = (int) strtol (str ,NULL,16); return num; } int main() { FILE* fptr, *outfptr; //char extension[] = ""; //unsigned char c1,c2; //unsigned char outC; int outBits; int byteWidth=0; char *file_name; // input file name char *file_nameOut; //out put file name file_name = (char*) malloc(256*sizeof(char)); printf("\n\nFile name :\t"); // get input file and open it scanf("%s",file_name); fptr=fopen(file_name,"r"); printf("\n Enter byte width: "); scanf("%i",&byteWidth); if (!fptr) { // exit if input file does not exist printf("\nERROR - File Note Found\n"); return 1; } file_nameOut = OPFileName(file_name); //make o/p file name printf("%s %s \n",file_name,file_nameOut); //debugging code outfptr=fopen(file_nameOut,"wb"); //open o/p file while (!feof(fptr)){ // writes byte to o/p file outBits = buildInt(fptr,byteWidth); fwrite( &outBits, byteWidth, 1, outfptr ) ; } fclose(fptr); fclose(outfptr); return 0; }