对于在Unix和Windows上都能运行的C中的64位整数(uint64_t),atoi是等效的吗?

Jos*_*phH 32 c c++ atoi

我正在尝试将64位整数字符串转换为整数,但我不知道使用哪一个.

cni*_*tar 36

使用strtoull,如果你有,或_strtoui64()与Visual Studio.

unsigned long long strtoull(const char *restrict str,
       char **restrict endptr, int base);


/* I am sure MS had a good reason not to name it "strtoull" or
 * "_strtoull" at least.
 */
unsigned __int64 _strtoui64(
   const char *nptr,
   char **endptr,
   int base 
);
Run Code Online (Sandbox Code Playgroud)


Fle*_*exo 18

你已经标记了这个问题,所以我假设你也可能对C++解决方案感兴趣.您可以使用boost::lexical_caststd::istringstream无法使用boost 来执行此操作:

#include <boost/lexical_cast.hpp>
#include <sstream>
#include <iostream>
#include <cstdint>
#include <string>

int main() {
  uint64_t test;
  test = boost::lexical_cast<uint64_t>("594348534879");

  // or
  std::istringstream ss("48543954385");
  if (!(ss >> test))
    std::cout << "failed" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这两种样式都适用于Windows和Linux(以及其他).

在C++ 11中,还有一些可以使用的函数std::string,包括std::stoull你可以使用的函数:

#include <string>

int main() {
  const std::string str="594348534879";
  unsigned long long v = std::stoull(str);
}
Run Code Online (Sandbox Code Playgroud)


Dmi*_*tri 7

就像是...

#ifdef WINDOWS
  #define atoll(S) _atoi64(S)
#endif
Run Code Online (Sandbox Code Playgroud)

..然后才使用atoll().您可能希望将其更改#ifdef WINDOWS为其他内容,只需使用您可以依赖的内容来指示atoll()缺少但atoi64()存在(至少对于您关注的方案).