C++: Convert std::string to UINT64

George G picture George G · Jul 19, 2016 · Viewed 7.7k times · Source

I need to convert a (decimal, if it matters) string representation of a number input from a text file to a UINT64 to pass to my data object.

    size_t startpos = num.find_first_not_of(" ");
    size_t endpos = num.find_last_not_of(" ");
    num = num.substr(startpos, endpos-startpos+1);
    UINT64 input;
    //convert num to input required here

Is there any way to convert an std::string to a UINT64 in a similar way to atoi()?

Thanks!

Edit: Working code below.

size_t startpos = num.find_first_not_of(" ");
size_t endpos = num.find_last_not_of(" ");
num = num.substr(startpos, endpos-startpos+1);
UINT64 input; //= std::strtoull(num.cstr(), NULL, 0);

std::istringstream stream (num);
stream >> input;

Answer

user1593881 picture user1593881 · Jul 19, 2016

Use strtoull or _strtoui64(). Example:

std::string s = "1123.45";
__int64 n = std::strtoull(s.c_str(),NULL,0);