📄 strtoul.c
字号:
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
unsigned long strtoul(CONST char *s, char **end, int base)
{
int endchar;
int enddigit;
unsigned long oldval;
unsigned long val = 0;
while (isspace(*s))
s++;
if (*s == 0 || !(base == 0 || (1 < base && base <= 36)))
{
if (end)
*end = (char *)s;
return 0;
}
if (*s == '+')
s++;
if (s[0] == '0' && (s[1] == 'X' || s[1] == 'x') &&
(base == 16 || base == 0))
{
base = 16;
s += 2;
}
if (base == 0)
base = (*s == '0') ? 8 : 10;
enddigit = (base >= 10) ? '9': base - 1 + '0';
if (base > 10)
endchar = base - 11 + 'a';
for ( ; *s; s++)
{
int c;
if (isdigit(*s) && *s <= enddigit)
c = *s - '0';
else if (base > 10 && (c = tolower(*s)) >= 'a' && c <= endchar)
c = c - 'a' + 10;
else
break;
oldval = val;
val = val * base + c;
if (val < oldval)
{
errno = ERANGE;
return ULONG_MAX;
}
}
if (end)
*end = (char *)s;
return val;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -