C++ convert string to uint64_t

42,035

Solution 1

Try this:

#include <iostream>
#include <sstream>
#include <cstdint>

int main() {
    uint64_t value;
    std::istringstream iss("18446744073709551610");
    iss >> value;
    std::cout << value;
}

See Live Demo


That may work for out of date standards too.

Solution 2

Try std::stoull if you are using C++11 or greater.

This post may also be of help. I didnt mark this as a duplicate because the other question is about C.

Solution 3

You can use strtoull() from <cstdlib> if you are using C++11 or newer. Else if you need this with c99 as well, you can go for strtoull() function in stdlib.h from C.

See the following example

#include <iostream>
#include <string>
#include <cstdlib> 

int main()
{
  std::string value= "14443434343434343434";
  uint64_t a;
  char* end;
  a= strtoull( value.c_str(), &end,10 );
  std::cout << "UInt64: " << a << "\n";
}

Solution 4

If you're using boost, you could make use of boost::lexical_cast

#include <iostream>
#include <string>
#include <boost-1_61/boost/lexical_cast.hpp> //I've multiple versions of boost installed, so this path may be different for you

int main()
{
    using boost::lexical_cast;
    using namespace std;

    const string s("2424242");
    uint64_t num = lexical_cast<uint64_t>(s);
    cout << num << endl;

    return 0;
}

Live example: http://coliru.stacked-crooked.com/a/c593cee68dba0d72

Solution 5

All these solutions didn't fit my need.

  • istringstream - parsing the string "123asd" to 123.
  • stoull - will raise and error and I didn't want to use try catch.
  • And boost wasn't used at the time.

So I just use a for loop:

uint64_t val = 0;
for (auto ch: new_str) {
    if (not isdigit(ch)) return 0;
    val = 10 * val + (ch - '0');
}

edit: Another problem is over flow, if the string is a bigger number than uint64_t. I added another starting if to check the number of the chars in the string.

Share:
42,035

Related videos on Youtube

Cauchy
Author by

Cauchy

Updated on July 09, 2022

Comments

  • Cauchy
    Cauchy almost 2 years

    I am trying to convert from a string to a uint64_t integer. stoi returns a 32 bit integer, so it won't work in my case. Are there other solutions?

  • Cauchy
    Cauchy about 7 years
    I'm not using boost
  • πάντα ῥεῖ
    πάντα ῥεῖ about 7 years
    Well, c++11 can be considered the current standard since several years now.
  • Gambit
    Gambit about 7 years
    It is a standard, but not THE standard. There are still a lot of people who are working with C++03 because of work or legacy code or whatever reason.
  • Tristan Brindle
    Tristan Brindle about 7 years
    Surely THE standard is C++14?
  • Cauchy
    Cauchy about 7 years
    @Gambit you are returning an unsigned long long which is not a uint64_t, which is not necessarily 64 bits depending on where it is being compiled. why is your solution still valid if unsigned long long is not 64 bits.
  • πάντα ῥεῖ
    πάντα ῥεῖ about 7 years
    @Gambit "but not THE standard" It's the current standard, I didn't say more. I even upvoted your answer dude!
  • M.M
    M.M about 7 years
    @Cauchy it must be at least 64 bits, therefore it can hold a 64-bit integer
  • πάντα ῥεῖ
    πάντα ῥεῖ about 7 years
    @BaummitAugen Hhhhhm, how does lexical_cast jump in to be useful here?
  • Baum mit Augen
    Baum mit Augen about 7 years
    @πάνταῥεῖ Are you asking me why lexical_cast is useful here or is this a request to OP to include an example?
  • πάντα ῥεῖ
    πάντα ῥεῖ about 7 years
    @Baum Both in case of doubt
  • Baum mit Augen
    Baum mit Augen about 7 years
    @πάνταῥεῖ The latter is the answer owner's job, but for the former: boost::lexical_cast is incredibly easy to use and (at least according to their own benchmark, freshest number with measured with gcc6.1), quite a lot faster than both stringstream and scanf + friends (for the conversion at hand). So if you are using boost anyways, a good method for this conversion.
  • Vada Poché
    Vada Poché about 7 years
    @πάνταῥεῖ Not a dead couch potato, but probably have more of a life than you. Assuming we're done with personal insults, the original link I posted had examples on that very page in case you hadn't clicked it. It looks like the answer was updated with a link to the "latest docs", which I am sure was done with good intentions, but that page does not have examples.
  • πάντα ῥεῖ
    πάντα ῥεῖ about 7 years
    @Vada That doesn't hinder you to improve your answer according to be self contained and showing a good and reusable code example.
  • Baum mit Augen
    Baum mit Augen about 7 years
    No need for the .c_str(), you can use the std::string directly. That's probably also faster.
  • davidvandebunte
    davidvandebunte over 5 years
    As you can confirm with a minor edit to the live demo in the answer, std::istringstream returns 0 if you provide a string that is not an integer. std::stoull throws an std::invalid_argument: Live Demo.
  • Darklighter
    Darklighter over 4 years
    @davidvandebunte You can either check that the extraction was successful by asserting the returned stream from iss >> value compares to true or you can enable exceptions on the stream via iss.exceptions(std::ios::failbit | std::ios::badbit);.
  • ChemiCalChems
    ChemiCalChems about 4 years
    I do not at all see how this could possibly be a better solution to the accepted answer. It's longer, more complicated to understand, and more error-prone.
  • Kyrol
    Kyrol about 4 years
    Ca you please explain more about what are you trying to achieve instead just posting code ?
  • Hiloliddin Jaloliddinzoda
    Hiloliddin Jaloliddinzoda about 4 years
    @ChemiCalChems It will directly convert String to 64 bit. I also searched many websites and could not find a good working method for esp8266 c code. So lastly I wrotemyself. Ideally it can convert any string to any base just by changing a bit. But here specifically it converts to 64bit.
  • Hiloliddin Jaloliddinzoda
    Hiloliddin Jaloliddinzoda about 4 years
    @Kyrol here what I tried to do is to go one by to to each individual Char of String convert it to hex integer. then ad it value. If you read carefully code you must understand what is done. It worked perfect for me. After 4 days of frustrated research without result I wrote this.
  • Kyrol
    Kyrol about 4 years
    @HiloliddinJaloliddinzoda Thanks for the explanation and the comments. +1 because, even if it's not C++, it's still a nice solution.
  • Avizipi
    Avizipi over 2 years
    actually std::istringstream will not return 0 if the string starts with digits. It will return the digits till the first non digit char.