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