int _atoi (const char * str, int base)
{
int val = 0, i, j = 1;
int digit, sign = 0;
int len = strlen (str);
if (len > 1) {
if (str[0] == '-') {
sign = 1;
str++;
len--;
} else if (str[0] == '+') {
sign = 0;
str++;
len--;
} else {
sign = 0;
}
}
for (i = (len - 1); i >= 0; i--)
{
if ((str[i] >= '0') && (str[i] <= '9')) {
digit = str[i] - '0';
} else if ((str[i] >= 'A') && (str[i] <= 'F')) {
digit = str[i] - 'A';
} else if ((str[i] >= 'a') && (str[i] <= 'f')) {
digit = str[i] - 'a';
} else {
break;
}
val += (digit * j);
j *= base;
}
if (sign) {
val *= -1;
}
return val;
}
int main(int argc, char* argv[])
{
char num [10];
int i = _atoi ("-102", 10);
printf ("Number : %d\n", i);
}
About our authors: Team EQA
You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.
‹
#
›