What is atoi equivalent for 64bit integer(uint64_t) in C that works on both Unix and Windows?

前端 未结 7 1527
失恋的感觉
失恋的感觉 2021-02-02 08:02

I\'m trying to convert 64bit integer string to integer, but I don\'t know which one to use.

7条回答
  •  故里飘歌
    2021-02-02 08:20

    You've tagged this question c++, so I'm assuming you might be interested in C++ solutions too. You can do this using boost::lexical_cast or std::istringstream if boost isn't available to you:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main() {
      uint64_t test;
      test = boost::lexical_cast("594348534879");
    
      // or
      std::istringstream ss("48543954385");
      if (!(ss >> test))
        std::cout << "failed" << std::endl;
    }
    

    Both styles work on Windows and Linux (and others).

    In C++11 there's also functions that operate on std::string, including std::stoull which you can use:

    #include 
    
    int main() {
      const std::string str="594348534879";
      unsigned long long v = std::stoull(str);
    }
    

提交回复
热议问题