ivy-jie

progress ...

C++博客 首页 新随笔 联系 聚合 管理
  9 Posts :: 41 Stories :: 6 Comments :: 0 Trackbacks
1 使用中
  int ahextoi( const char* hex_str)
{
   int r =0;
  if(hex_str)
     sscanf(hex_str,"%x",&r);
   return r;
}
2 没试过
 //将任意进制(2~36)的数转换为10进制,参数step=16就是你想要的
 unsigned long AToD(string dvalue,unsigned step/*2~36*/)
{
  if(step <2 || step > 36) return NULL;

  unsigned long result =0;
  unsigned maxPos = dvalue.length();
  unsigned tv;
  for(unsigned index=0;index < maxPos; index++)
 {
   tv = dvalue[index];
   if(isalpha(tv))
     {
                if(islower(tv) tv=toupper(tv);
                tv -= char(‘A');
                tv+= 10;
                if(tv > step) return unsigned (-1);
     }
     else if(isdigit(tv))
     { 
                tv -= char('0');
                if(tv > step) return unsigned (-1);
      }
      else return unsigned (-1); // invalid string
      result = tv * pow(step, maxPos-index-1); // result = sum * step + tv;
}// end for
 return result;
}
posted on 2009-10-07 11:17 ivy-jie 阅读(4404) 评论(0)  编辑 收藏 引用 所属分类: arithmetic

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理