int HexToInt(const char* phex) { int hi,lo; if(::isdigit(*phex)) hi = *phex-'0'; else hi = *phex-'A'+10; if(::isdigit(*(phex+1))) lo = *(phex+1)-'0'; else lo = *(phex+1)-'A'+10; return hi*16+lo; } ////使用的时候是这样的 int i = HexToInt("2F");///注意,这里传递的字符串应该大写, 对于小写的处理,你可以自行添加。 对于一大串字符串,你可以分隔开来,再一个一个处理。
有标准的C函数: sscanf //include <stdio.h>
example: char onestrin[]="23ad"; int c; sscanf(onestring,"%x",&c);//c=0x23ad
{
int hi,lo;
if(::isdigit(*phex))
hi = *phex-'0';
else
hi = *phex-'A'+10;
if(::isdigit(*(phex+1)))
lo = *(phex+1)-'0';
else
lo = *(phex+1)-'A'+10;
return hi*16+lo;
}
////使用的时候是这样的
int i = HexToInt("2F");///注意,这里传递的字符串应该大写,
对于小写的处理,你可以自行添加。
对于一大串字符串,你可以分隔开来,再一个一个处理。
sscanf //include <stdio.h>
example:
char onestrin[]="23ad";
int c;
sscanf(onestring,"%x",&c);//c=0x23ad