如何将16进制字符串转换为数字
在实际使用中,我们经常需要将16进制字符串转换为数字,具体代码如下
#define isdigit(c) (('0' <= (c) && (c) <= '9'))
#define isalpha(c) ((('a' <= (c) && (c) <= 'z')) || (('A' <= (c) && (c) <= 'Z')))
#define toupper(x) ((((x) >= 'A') && ((x) <= 'Z')) ? (x) : ((x) - 'a' + 'A'))
int hexToNum(char ch)
{
ch = toupper(ch);
if ( isdigit(ch) )
{
ch -= '0';
}
else if ( isalpha(ch) )
{
ch -= 'A';
ch += 0x0A;
}
else
{
printf("%02x is not a hex digit",ch);
}
return ch;
}