Files
c_progarm/other/mystring.c
2023-12-02 11:52:15 +08:00

75 lines
823 B
C

/*
*
* 把整数字符串传化为int,直到遇到非数字字符
*
*/
static int str_ainttoi(const char *s)
{
int ret=0;
int sig=1;
if(*s=='-'){
s++;
sig=-1;
}
while(*s)
{
if(*s>='0'&&*s<='9')
{
ret*=10;
ret+=*s-'0';
}
else return ret;
s++;
}
return ret*sig;
}
int str_ahextoi(const char *s)
{
int ret=0;
while(*s)
{
if(*s>='0'&&*s<='9')
{
ret*=16;
ret+=*s-'0';
}
else if(*s>='a'&&*s<='f')
{
ret*=16;
ret+=*s-'a'+10;
}
else if(*s>='A'&&*s<='F')
{
ret*=16;
ret+=*s-'A'+10;
}
else return ret;
s++;
}
return ret;
}
int str_atoi(const char *s)
{
if(s[0]=='0'&&((s[1]=='x')||(s[1]=='X'))){
return str_ahextoi(&s[2]);
}else{
return str_ainttoi(s);
}
}