Tuesday, October 20, 2009

Convert binary file to ASCII file

/************************************************/
/* Convert binary file to ASCII file */
/************************************************/


#include < D:/VC6Green/VC98/Include/math.h>
#include < D:/VC6Green/VC98/Include/stdio.h>
#include < D:/VC6Green/VC98/Include/string.h>



void __ConvertBinFile_To_ASCIIFile(void *ptrInRd, void *ptrOutWr)
{
unsigned char lcByteIn,i;
FILE *ptrRd,*ptrWr;

ptrRd=(FILE *)(ptrInRd);
ptrWr=(FILE *)(ptrOutWr);


i=0;

fprintf(ptrWr, " ");

while (1)
{
lcByteIn = fgetc(ptrRd);
if(feof(ptrRd)!=0)
break;
printf("%02X ",lcByteIn); /* O/P the character to the screen */
fprintf(ptrWr, "%02X ",lcByteIn);
i++;
if (i==16)
{
printf("\n");
fprintf(ptrWr, "\n ");
i=0;
}
}

fclose(ptrRd); /* Close the file. */
fclose(ptrWr); /* Close the file. */

printf("\n");
printf("////////////Done Bin To ASCII conversion //////////// \n");

}




main()
{
FILE *fileRdPtr, *fileWrtPtr;

/* Open Binary file for output. */
fileWrtPtr = fopen("Result.txt","w");
fileRdPtr = fopen("BinaryFile2Convert.bin", "rb");

if(fileRdPtr==NULL)
{
printf("Could not find input binary file: BinaryFile2Convert.bin \n");
return 0;
}

printf("Pls make sure binary input file is using file name: 'BinaryFile2Convert.bin'. Output file is: 'Result.txt'\n");


__ConvertBinFile_To_ASCIIFile(fileRdPtr,fileWrtPtr);

return 0;


}

No comments: