标签:
/*========================================================*/
/*
RainLeaf 文件转化工具 [版本 1.0]/n(C) 版权所有 2008-2009 R.L Studio.
功能:将一个文件转化为十六进制文件,或者将一个十六进制文件转化为二进制文件
条件:输入文件必须存在
方法:CONVERTFILE [-h | -f]
CONVERTFILE -h :将输入二进制文件转化为十六进制文件
CONVERTFILE -f :将输入十六进制文件转化为二进制文件
------------------------------------------------------------
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);
/*========================================================*/
/*
主函数
*/
int main(int argc,char **argv)
{
char ch[30];
if(argc < 2 || argc > 2)
{
printf("RainLeaf 文件转化工具 [版本 1.0]/n(C) 版权所有 2008-2009 R.L Studio.\n\n");
printf("功能:将一个文件转化为十六进制文件,或者将一个十六进制文件转化为二进制文件\n");
printf("条件:输入文件必须存在\n");
printf("方法:CONVERTFILE [-h | -f]\n\n");
printf("CONVERTFILE -h :将输入二进制文件转化为十六进制文件\n");
printf("CONVERTFILE -f :将输入十六进制文件转化为二进制文件\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("输入参数错误,请直接输入命令查看帮助.\n");break;
}
return 0;
}
/*========================================================*/
/*
功能:二进制文件转化位十六进制文件
参数:无
返回:成功:0;失败:-1
*/
int FileToHex()
{
FILE *input;
FILE *output;
char infile[30],outfile[30];
unsigned char temp;
char high,low;
int i=0;
printf("输入文件名:");
gets(infile);
printf("输出文件名:");
gets(outfile);
if((input = fopen(infile,"rb")) == NULL || (output = fopen(outfile,"w")) == NULL)
{
return -1;
}
fprintf(output,"char MainDL_Ac49x[] = {\n\r");
temp = fgetc(input);
while(!feof(input))
{
i++;
low = temp & 0x0F;
high = temp >> 4;
fprintf(output,"(char)0x%x%x",high,low);
if(i%8==0)
fprintf(output,"\n");
temp = fgetc(input);
if(!feof(input))
fprintf(output,", ");
}
fprintf(output,"\n}");
printf("二进制文件%s成功转化位十六进制文件%s.\n",infile,outfile);
fclose(input);
fclose(output);
return 0;
}
/*========================================================*/
/*
功能:十六进制文件转化位二进制文件
参数:无
返回:成功:0;失败:-1
*/
int HexToFile()
{
FILE *input;
FILE *output;
FILE *Temp;
char infile[30],outfile[30];
unsigned char temp;
unsigned char high,low;
printf("输入文件名:");
gets(infile);
printf("输出文件名:");
gets(outfile);
if((input = fopen(infile,"r")) == NULL || (Temp = fopen("$$","w")) == NULL)
{
return -1;
}
/* 过滤源十六进制文件 */
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("十六进制文件%s成功转化位二进制文件%s.",infile,outfile);
fclose(input);
fclose(output);
/* 删除临时文件 */
remove("$$");
return 0;
}
/*========================================================*/
/*
功能:取得传入字符的索引值,也就是对应的二进制
参数:字符(0~f)
返回:成功:0~15;失败:-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‘)?10:((sour == ‘b‘||sour == ‘B‘)?11:((sour == ‘c‘||sour == ‘C‘)?12:((sour == ‘d‘||sour == ‘D‘)?13:((sour == ‘e‘||sour == ‘E‘)?14:((sour == ‘f‘||sour == ‘F‘)?15:-1)))));
return i;
}
/*========================================================*/
/*
功能:判断字符是否是十六进制合法字符
参数:字符(0~f)
返回:成功:1~16;失败: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‘)?10:((sour == ‘b‘||sour == ‘B‘)?11:((sour == ‘c‘||sour == ‘C‘)?12:((sour == ‘d‘||sour == ‘D‘)?13:((sour == ‘e‘||sour == ‘E‘)?14:((sour == ‘f‘||sour == ‘F‘)?15:-1)))));
return (i+1);
}
标签:
原文地址:http://www.cnblogs.com/qunxuan/p/4705441.html