- /*========================================================*/
- /*
- Rainleaf File Conversion Tool [version 1.0]/n (C) Copyright 2008-2009 R.L Studio.
- Function: Convert a file to a hexadecimal file, or convert a hexadecimal file to a binary file
- Condition: The input file must exist
- Method: Convertfile [-H |-F]
- Convertfile-h: Converting an input binary file to a hexadecimal file
- Convertfile-f: Converting an input hex file into a binary file
- ------------------------------------------------------------
- Compiler:
- Visual C + + 6.0
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- Char getindex (char sour);
- int Hextofile ();
- int Filetohex ();
- int Ishex (char sour);
- /*========================================================*/
- /*
- Main function
- */
- int main (int argc,char **argv)
- {
- Char ch[30];
- if (argc < 2 | | argc > 2)
- {
- printf ("rainleaf File Conversion Tool [version 1.0]/n (C) Copyright 2008-2009 r.l studio./n/n");
- printf ("function: Convert a file to a hexadecimal file, or convert a hexadecimal file to a binary file/n");
- printf ("condition: Input file must exist/n");
- printf ("method: Convertfile [-H | -f]/n/n ");
- printf ("convertfile-h: Convert the input binary file to a hex file/n");
- printf ("convertfile-f: Convert the input hex file into a binary file/n/n");
- return 0;
- }
- strcpy (Ch,argv[argc-1]);
- switch (ch[1])
- {
- case ' h ':;
- case ' H ': Filetohex (); Break
- case ' F ':;
- case ' F ': Hextofile (); Break
- default:printf ("input parameter error, please enter command directly to see Help./n"); Break
- }
- return 0;
- }
- /*========================================================*/
- /*
- Function: Binary file conversion bit hex file
- Parameters: None
- Return: Success: 0; failure: 1
- */
- int Filetohex ()
- {
- FILE *input;
- FILE *output;
- Char infile[30],outfile[30];
- unsigned char temp;
- Char High,low;
- printf ("Input file name:");
- Gets (infile);
- printf ("Output file name:");
- Gets (outfile);
- if (input = fopen (infile,"RB")) = = NULL | | (output = fopen (outfile,"w")) = = = NULL)
- {
- return-1;
- }
- temp = fgetc (input);
- While (!feof (input))
- {
- Low = temp & 0x0F;
- High = temp >> 4;
- fprintf (output,"%x%x", High,low);
- temp = fgetc (input);
- }
- printf ("binary file%s successfully converted bit hex file%s.", Infile,outfile);
- Fclose (input);
- fclose (output);
- return 0;
- }
- /*========================================================*/
- /*
- Function: Hex file conversion bit binary file
- Parameters: None
- Return: Success: 0; failure: 1
- */
- int Hextofile ()
- {
- FILE *input;
- FILE *output;
- FILE *temp;
- Char infile[30],outfile[30];
- unsigned char temp;
- unsigned char high,low;
- printf ("Input file name:");
- Gets (infile);
- printf ("Output file name:");
- Gets (outfile);
- if (input = fopen (infile,"R")) = = NULL | | (Temp = fopen ("$$","w")) = = = NULL)
- {
- return-1;
- }
- / * Filter Source Hex file * /
- temp = fgetc (input);
- While (!feof (input))
- {
- if (Ishex (temp))
- {
- FPUTC (temp,temp);
- temp = fgetc (input);
- }
- Else
- temp = fgetc (input);
- }
- Fclose (input);
- Fclose (Temp);
- if (input = fopen ("$$","R")) = = NULL | | (output = fopen (outfile,"wb") = = = NULL)
- {
- return-1;
- }
- High = fgetc (input);
- Low = fgetc (input);
- While (!feof (input))
- {
- High = GetIndex (high);
- Low = GetIndex (low);
- if (high = =-1 | | low = =-1)
- return-1;
- High = high << 4;
- temp = High | Low
- FPUTC (Temp,output);
- High = fgetc (input);
- Low = fgetc (input);
- }
- printf ("hex file%s successfully converted bit binary file%s.", Infile,outfile);
- Fclose (input);
- fclose (output);
- / * Delete temporary files * /
- Remove ("$$");
- return 0;
- }
- /*========================================================*/
- /*
- Function: Gets the index value of the passed-in character, which is the corresponding binary
- Parameter: Character (0~F)
- Return: Success: 0~15; failure: 1
- */
- Char getindex (char Sour)
- {
- int i;
- unsigned char *hex = "0123456789";
- For (i = 0;i < strlen (hex); i++)
- if (hex[i] = = Sour)
- return i;
- i = (Sour = = ' A ' | | Sour = = ' A ')?: ((Sour = = ' B ' | | Sour = = ' B ')?: ((Sour = = ' C ' | | Sour = = ' C ')?: ((Sour = = ' d ' | | Sour = = ' D ')?: ((Sour = = ' E ' | | Sour = = ' E ')?: ((Sour = = ' F ' | | Sour = = ' F ')? 15:-1)));
- return i;
- }
- /*========================================================*/
- /*
- Function: Determines whether a character is a hexadecimal legal character
- Parameter: Character (0~F)
- Return: Success: 1~16; failed: 0
- */
- int Ishex (char Sour)
- {
- int i;
- unsigned char *hex = "0123456789";
- For (i = 0;i < strlen (hex); i++)
- if (hex[i] = = Sour)
- return i+1;
- i = (Sour = = ' A ' | | Sour = = ' A ')?: ((Sour = = ' B ' | | Sour = = ' B ')?: ((Sour = = ' C ' | | Sour = = ' C ')?: ((Sour = = ' d ' | | Sour = = ' D ')?: ((Sour = = ' E ' | | Sour = = ' E ')?: ((Sour = = ' F ' | | Sour = = ' F ')? 15:-1)));
- return (i+1);
- }
Any binary and hexadecimal conversion tools