16進制字串轉數值
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int strtonum(char *p_input)
{
char *pTmp;
if ( *p_input == '0' )
{
if ( *(p_input + 1) == 'x' ||
*(p_input + 1) == 'X' )
{
pTmp = p_input + 2;
return strtoll( pTmp, NULL, 16 );
}
}
return strtoll( p_input, NULL, 10 );
}
int main(int argc, char *argv[])
{
char val[] = "0x30";
int ret = strtonum(val);
printf("%s = 0x%x\n", val, ret);
return 0;
}
Result:
0x30 = 0x30
沒有留言:
張貼留言