Any binary and hexadecimal conversion tools

Source: Internet
Author: User

  1. /*========================================================*/
  2. /*
  3. Rainleaf File Conversion Tool [version 1.0]/n (C) Copyright 2008-2009 R.L Studio.
  4. Function: Convert a file to a hexadecimal file, or convert a hexadecimal file to a binary file
  5. Condition: The input file must exist
  6. Method: Convertfile [-H |-F]
  7. Convertfile-h: Converting an input binary file to a hexadecimal file
  8. Convertfile-f: Converting an input hex file into a binary file
  9. ------------------------------------------------------------
  10. Compiler:
  11. Visual C + + 6.0
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. Char getindex (char sour);
  17. int Hextofile ();
  18. int Filetohex ();
  19. int Ishex (char sour);
  20. /*========================================================*/
  21. /*
  22. Main function
  23. */
  24. int main (int argc,char **argv)
  25. {
  26. Char ch[30];
  27. if (argc < 2 | | argc > 2)
  28. {
  29. printf ("rainleaf File Conversion Tool [version 1.0]/n (C) Copyright 2008-2009 r.l studio./n/n");
  30. printf ("function: Convert a file to a hexadecimal file, or convert a hexadecimal file to a binary file/n");
  31. printf ("condition: Input file must exist/n");
  32. printf ("method: Convertfile [-H |  -f]/n/n ");
  33. printf ("convertfile-h: Convert the input binary file to a hex file/n");
  34. printf ("convertfile-f: Convert the input hex file into a binary file/n/n");
  35. return 0;
  36. }
  37. strcpy (Ch,argv[argc-1]);
  38. switch (ch[1])
  39. {
  40. case ' h ':;
  41. case ' H ': Filetohex ();   Break
  42. case ' F ':;
  43. case ' F ': Hextofile ();   Break
  44. default:printf ("input parameter error, please enter command directly to see Help./n");   Break
  45. }
  46. return 0;
  47. }
  48. /*========================================================*/
  49. /*
  50. Function: Binary file conversion bit hex file
  51. Parameters: None
  52. Return: Success: 0; failure: 1
  53. */
  54. int Filetohex ()
  55. {
  56. FILE *input;
  57. FILE *output;
  58. Char infile[30],outfile[30];
  59. unsigned char temp;
  60. Char High,low;
  61. printf ("Input file name:");
  62. Gets (infile);
  63. printf ("Output file name:");
  64. Gets (outfile);
  65. if (input = fopen (infile,"RB")) = = NULL | | (output = fopen (outfile,"w")) = = = NULL)
  66. {
  67. return-1;
  68. }
  69. temp = fgetc (input);
  70. While (!feof (input))
  71. {
  72. Low = temp & 0x0F;
  73. High = temp >> 4;
  74. fprintf (output,"%x%x", High,low);
  75. temp = fgetc (input);
  76. }
  77. printf ("binary file%s successfully converted bit hex file%s.", Infile,outfile);
  78. Fclose (input);
  79. fclose (output);
  80. return 0;
  81. }
  82. /*========================================================*/
  83. /*
  84. Function: Hex file conversion bit binary file
  85. Parameters: None
  86. Return: Success: 0; failure: 1
  87. */
  88. int Hextofile ()
  89. {
  90. FILE *input;
  91. FILE *output;
  92. FILE *temp;
  93. Char infile[30],outfile[30];
  94. unsigned char temp;
  95. unsigned char high,low;
  96. printf ("Input file name:");
  97. Gets (infile);
  98. printf ("Output file name:");
  99. Gets (outfile);
  100. if (input = fopen (infile,"R")) = = NULL | | (Temp = fopen ("$$","w")) = = = NULL)
  101. {
  102. return-1;
  103. }
  104. / * Filter Source Hex file * /
  105. temp = fgetc (input);
  106. While (!feof (input))
  107. {
  108. if (Ishex (temp))
  109. {
  110. FPUTC (temp,temp);
  111. temp = fgetc (input);
  112. }
  113. Else
  114. temp = fgetc (input);
  115. }
  116. Fclose (input);
  117. Fclose (Temp);
  118. if (input = fopen ("$$","R")) = = NULL | | (output = fopen (outfile,"wb") = = = NULL)
  119. {
  120. return-1;
  121. }
  122. High = fgetc (input);
  123. Low = fgetc (input);
  124. While (!feof (input))
  125. {
  126. High = GetIndex (high);
  127. Low = GetIndex (low);
  128. if (high = =-1 | | low = =-1)
  129. return-1;
  130. High = high << 4;
  131. temp = High | Low
  132. FPUTC (Temp,output);
  133. High = fgetc (input);
  134. Low = fgetc (input);
  135. }
  136. printf ("hex file%s successfully converted bit binary file%s.", Infile,outfile);
  137. Fclose (input);
  138. fclose (output);
  139. / * Delete temporary files * /
  140. Remove ("$$");
  141. return 0;
  142. }
  143. /*========================================================*/
  144. /*
  145. Function: Gets the index value of the passed-in character, which is the corresponding binary
  146. Parameter: Character (0~F)
  147. Return: Success: 0~15; failure: 1
  148. */
  149. Char getindex (char Sour)
  150. {
  151. int i;
  152. unsigned char *hex = "0123456789";
  153. For (i = 0;i < strlen (hex); i++)
  154. if (hex[i] = = Sour)
  155. return i;
  156. 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)));
  157. return i;
  158. }
  159. /*========================================================*/
  160. /*
  161. Function: Determines whether a character is a hexadecimal legal character
  162. Parameter: Character (0~F)
  163. Return: Success: 1~16; failed: 0
  164. */
  165. int Ishex (char Sour)
  166. {
  167. int i;
  168. unsigned char *hex = "0123456789";
  169. For (i = 0;i < strlen (hex); i++)
  170. if (hex[i] = = Sour)
  171. return i+1;
  172. 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)));
  173. return (i+1);
  174. }

Any binary and hexadecimal conversion tools

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.