2011年4月15日 星期五

string to unsigned long---strtoul

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
{
    unsigned long result = 0,value;

    if (*cp == '0') {
        cp++;
        if ((*cp == 'x') && isxdigit(cp[1])) {
            base = 16;
            cp++;
        }
        if (!base) {
            base = 8;
        }
    }
    if (!base) {
        base = 10;
    }
    while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
        ? toupper(*cp) : *cp)-'A'+10) < base) {
        result = result*base + value;
        cp++;
    }
    if (endp)
        *endp = (char *)cp;
    return result;
}

int main(void)
{
    char *s="0xc0008000";
    unsigned long abc=0;

    abc=simple_strtoul(s,NULL,16);

    printf("abc=%lx\n", abc);

    return 0;
}


結果:
abc=c0008000

類似:
strtod

【轉貼】 uboot source code

沒有留言:

張貼留言