I'm trying to convert 64bit integer string to integer, but I don't know which one to use.
The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number.
atoi stands for ASCII to integer. It is included in the C standard library header file stdlib.
The atoi() function in C++ is defined in the cstdlib header. It accepts a string parameter that contains integer values and converts the passed string to an integer value.
First, atoi() converts C strings (null-terminated character arrays) to an integer, while stoi() converts the C++ string to an integer. Second, the atoi() function will silently fail if the string is not convertible to an int , while the stoi() function will simply throw an exception.
Use strtoull
if you have it or _strtoui64()
with 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 );
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 <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; }
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 <string> int main() { const std::string str="594348534879"; unsigned long long v = std::stoull(str); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With