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

2020-09-08 10:04:29 浏览数 (1)

代码语言:javascript复制
/* 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 = %sn", pEnd);
    li2 = strtol (pEnd, &pEnd, 16);
    printf("pEnd = %sn", pEnd);
    li3 = strtol (pEnd, &pEnd, 2);
    printf("pEnd = %sn", 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是转换后剩余的字符串

0 人点赞