/****************************************************************************** * * Title : hexToBinFileConverison.c * Version 0.0, 20 October 2000, * * Description: This program converts hex text file (serial byte stream) into binary file. * Input: Hex text file fileName.hex * Output: Binary file fileName.bin * Note: The conversion ends when input character is not hex number. * * Author: Lukas Tomasek, tomasekl@fzu.cz * ******************************************************************************/ /****************************************************************************** * Header files * ******************************************************************************/ #include #include /****************************************************************************** * Definitions * ******************************************************************************/ #define NOT_VALID_HEX_NUMBER (-1) #define SUCCESS (0) /****************************************************************************** * Static Function Declarations * ******************************************************************************/ static int halfByteConversionHexToBin(char hexValue, unsigned char *binValue); /****************************************************************************** * Main * ******************************************************************************/ int main(int argc, char *argv[]){ FILE *hexFileHandle; FILE *binFileHandle; size_t bytesRead; char errorMessage[200]; int status; long hexFileSize, i; char hexFileName[300]; char binFileName[300]; char directory[300]; int hexValue; unsigned char binValue; unsigned char convertValue; int length, dirLength; memset(binFileName, 0, 300); memset(hexFileName, 0, 300); strcpy(hexFileName,argv[1]); /* get input hex file name (*.hex) */ printf("%s\n", hexFileName); /* create binary file name (*.bin) */ length=strlen(hexFileName)-4; strncpy(binFileName, hexFileName, length); strcat(binFileName,".bin"); printf("%s\n", binFileName); hexFileHandle = fopen (hexFileName, "r"); /* open text file for read */ binFileHandle = fopen (binFileName, "wb"); /* open binary file for write */ status=GetFileSize(hexFileName, &hexFileSize); printf("%d\n",hexFileSize); for(i=0;iend of conversion, not error! */ }; convertValue=binValue<<4; hexValue=fgetc(hexFileHandle); if(halfByteConversionHexToBin(hexValue, &binValue)!=SUCCESS){ break; /* non hex character->end of conversion, not error! */ }; convertValue+=binValue; fprintf(binFileHandle,"%c", convertValue); } /* close files */ status=fclose(hexFileHandle); status=fclose(binFileHandle); printf("----- PRESS ANY KEY -------"); getchar(); return(0); } /****************************************************************************** * Static functions * ******************************************************************************/ /*============================================================================= * halfByteConversionHexToBin() *============================================================================= * * Converts one hex character (range 0 to F) from input to output binary value * */ static int halfByteConversionHexToBin(char hexValue, unsigned char *binValue){ if((hexValue>='0')&&(hexValue<='9')){ *binValue=(unsigned char)(hexValue-'0'); }else{ if((hexValue>='A')&&(hexValue<='F')){ *binValue=(unsigned char)(hexValue-'A'+10); }else{ if((hexValue>='a')&&(hexValue<='f')){ *binValue=(unsigned char)(hexValue-'a'+10); }else{ return(NOT_VALID_HEX_NUMBER); /* input character is not valid hex number */ } } } return(SUCCESS); } /******************************************************************************/