int StrHexToDecimal(char * hexstr)
{
char ch;//用来接受一个输入字符
int stack[50];//存储处理后的十六进制对应的十进制
int top=0;//栈里面数据的个数
int decimal=0;//小数点在输入字符串中的位置,从0号位置开始计数
float num=0;//计算最后的十进制数
int i=0;
// printf("请输入十六进制(0-9,a-f,A-F):");
while ((ch=hexstr[i++])!=0)
{
if (ch>='0'&&ch<='9')
stack[top++]=int(ch-48);
else if (ch>='A'&&ch<='F')
stack[top++]=int(ch-55);
else if (ch>='a'&&ch<='f')
stack[top++]=int(ch-87);
else if (ch=='.')//如果是小数点
decimal=top;//表明从decimal个数据以后的数据都是小数部分
else {
printf("输入了非法数据");
return 0;
}
}
while(top)
{
if (decimal!=0&&top>decimal)//计算小数部分
num+=stack[--top]/pow(16,top-decimal+1);
else
num+=stack[--top]*pow(16,top);//计算整数部分
}
printf("%.4f",num);
return num;
}
int HexToDecimal(char * hexstr)
{
char ch;//用来接受一个输入字符
int stack[5];//存储处理后的十六进制对应的十进制
int top=0;//栈里面数据的个数
int num=0;//计算最后的十进制数
int i=0;
// printf("请输入十六进制(0-9,a-f,A-F):");
while ((ch=hexstr[i++])!=0)
{
if (ch>='0'&&ch<='9')
stack[top++]=int(ch-48);
else if (ch>='A'&&ch<='F')
stack[top++]=int(ch-55);
else if (ch>='a'&&ch<='f')
stack[top++]=int(ch-87);
else {
printf("输入了非法数据%x",ch);
return 0;
}
}
i-=2;
while(top)
{
num+=stack[--top]*pow(16,i-top);//计算整数部分
}
// printf("%d",num);
return num;
}
int main (int argc, char *argv[])
{
char buffer[81];
char tmpbuf[8];
int n1=2, n2=3, n3=5, i, j, memcnt;
FILE *fd;
FILE *F1;
fprintf(stderr, "FINDSTR Version 1.01: August 5, 2009/n/n");
// FindInAll("E://pro//HexString");
if ((fd = fopen("E://pro//HexString//chPwJs.c", "r")) == NULL) {
printf("BPNN_SAVE: Cannot create '%s'/n", "E://pro//HexString//chPwJs.c");
return 1;
}
if ((F1 = fopen("E://pro//HexString//sortresults.txt", "wt+")) == 0)
{
fprintf(stderr, "Can't find file: %s/n", "sortresults.txt");
return 1;
}
// do
// {
// i=fread(tmpbuf, 7, 1, fd);
// }while(i==7);
while ( !feof ( fd ) )
{
char * ptemp=tmpbuf;
for ( i=0; i<8; i++ )
{
char temp = getc ( fd );
if(temp==0x20 || temp==0x0D ||temp==0x0A)
continue;
/*putc ( getc(in), out );*/
if ( !feof ( fd ) && temp != ',' )
{
if(temp!=0x20)
*ptemp++ = temp;
}
else
{
char outchar;
*ptemp=0;
// outchar=atoi(&tmpbuf[2]);
outchar=HexToDecimal(&tmpbuf[2]);
printf("%c, ", outchar);
// printf("%s %c, ", tmpbuf,outchar);
if(outchar>0)
{
fprintf(F1,"%c", outchar);
}
break;
}
}
}
fclose(fd);
fclose(F1);
printf("Done/n");
return 1;
}