这是我的 C 代码:
char *ptr = "0xfff1342809062Cac";
char *pEnd;
long long value = strtoll(ptr, &pEnd, 0);
printf("%lld\n", value);
printf("errno: %d\n", errno);
Run Code Online (Sandbox Code Playgroud)
我是用gcc-8.3.0编译的,输出是:
9223372036854775807
errno: 34
Run Code Online (Sandbox Code Playgroud)
我很困惑 strtoll 给出了一个意想不到的值并将 errno 设置为 34。
这种行为是正确的。在您的系统的最大价值long long,即LLONG_MAX是9223372036854775807。
您的字符串中的值大于此值;如果值超出范围并且太大,则指定的行为是:返回LLONG_MAX并errno设置为ERANGE(在您的系统上大概是 34)。
也许考虑使用strtoull和unsigned long long返回值,因为该字符串适合该数据类型。