strtol函数的用法——字符串转长整形

/* strtol example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* strtol */

int main ()
{
    char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
    char *pEnd;
    long int li1, li2, li3, li4;
    li1 = strtol (szNumbers, &pEnd, 10);
    printf("pEnd = %s\n", pEnd);
    li2 = strtol (pEnd, &pEnd, 16);
    printf("pEnd = %s\n", pEnd);
    li3 = strtol (pEnd, &pEnd, 2);
    printf("pEnd = %s\n", pEnd);
    li4 = strtol (pEnd, NULL, 0);
    printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
    return 0;
}

运行结果:

pEnd =  60c0c0 -1101110100110100100000 0x6fffff
pEnd =  -1101110100110100100000 0x6fffff
pEnd =  0x6fffff
The decimal equivalents are: 2001, 6340800, -3624224 and 7340031.

从结果中可以看出,pEnd是转换后剩余的字符串

posted @ 2019-10-05 16:22  wdliming  阅读(530)  评论(0编辑  收藏  举报