#include<iostream>
using namespace std;
const int int_max = 2147483647;
const int int_min = -2147483648;
bool isAlph(char num)
{
if ((num >= 'A'&& num <= 'Z') || (num >= 'a'&& num <= 'z'))
return true;
else
return false;
}
bool isDigit(char num)
{
if ((num >= '0'&& num <= '9'))
return true;
else
return false;
}
char toLower(char num)
{
char result = num;
if (num >= 'A' && num <= 'Z')
result += 32;
return result;
}
int strtoLL(const char *num_str, char **endptr, int base)
{
long long result = 0;
long long value;
if (!base)
{
if (*num_str == '0')
{
num_str++;
if (*num_str == 'x' || *num_str == 'X')
{
base = 16;
num_str++;
}
else
base = 8;
}
else
base = 10;
}
while (true)
{
if (isAlph(*num_str) || isDigit(*num_str))
{
value = isAlph(*num_str) ? toLower(*num_str) - 'a' + 10 : *num_str - '0';
if (value >= base)
break;
result = result*base + value;
if (result > int_max)
result = int_max;
if (result < int_min)
result = int_min;
num_str++;
}
else
break;
}
if (endptr)
*endptr = const_cast<char*> (num_str);
return result;
}
int Mystrtol(const char *num_str, char **endptr, int base)
{
if (*num_str == '-')
{
long result = strtoLL(num_str + 1, endptr, base);
if (result == int_max)
return int_min;
else
return -result;
}
return strtoLL(num_str, endptr, base);
}
int main()
{
char *str = "0123456789ABCDEFGHItest";
char *temp;
cout << "******Mystrtol********" << endl;
int result = Mystrtol(str, &temp,0);
cout << result << endl;
cout << temp << endl;
cout << "******strtol**********" << endl;
result = strtol(str, &temp,0);
cout << result << endl;
cout << temp << endl;
system("pause");
return 0;
}