C++字符串转long long

偶尔会遇到的,记录一下。

/*
    字符串转long long
*/
long long atoll(const char *p)
{
    long long n;
    int c, neg = 0;
    unsigned char   *up = (unsigned char *)p;

    if (!isdigit(c = *up)) {
        while (isspace(c))
            c = *++up;
        switch (c) {
        case '-':
            neg++;
            /* FALLTHROUGH */
        case '+':
            c = *++up;
        }
        if (!isdigit(c))
            return (0);
    }

    for (n = '0' - c; isdigit(c = *++up); ) {
        n *= 10; /* two steps to avoid unnecessary overflow */
        n += '0' - c; /* accum neg to avoid surprises at MAX */
    }

    return (neg ? n : -n);
}

作者: 阿飞

出处: https://www.cnblogs.com/nxopen2018/>

关于作者:......

如有问题, 可在底部(留言)咨询.

原文地址:https://www.cnblogs.com/nxopen2018/p/14465772.html