Hex

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值