'C++ convert string to uint64_t

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?



Solution 1:[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:[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:[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:[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:[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.

Solution 6:[6]

NOTE: This is solution for c not for C++. So it maybe harder then in C++. Here we convert String HEX to uint64_t hex value. All individual characters of string is converted to hex integer ony by one. For example in base 10 -> String = "123":

  • 1st loop : value is 1
  • 2nd loop : value is 1*10 + 2 = 12
  • 3rd loop : value is 12*10 + 3 = 123

So like this logic is used to convert String of HEX character to uint_64hex value.

uint64_t stringToUint_64(String value) {
  int stringLenght = value.length();

  uint64_t uint64Value = 0x0;
  for(int i = 0; i<=stringLenght-1; i++) {
    char charValue = value.charAt(i);

    uint64Value = 0x10 * uint64Value;
    uint64Value += stringToHexInt(charValue);
  }

  return uint64Value;
}

int stringToHexInt(char value) {
  switch(value) {
    case '0':
      return 0;
      break;
    case '1':
      return 0x1;
      break;
    case '2':
      return 0x2;
      break;
    case '3':
      return 0x3;
      break;
    case '4':
      return 0x4;
      break;
    case '5':
      return 0x5;
      break;
    case '6':
      return 0x6;
      break;
    case '7':
      return 0x7;
      break;
    case '8':
      return 0x8;
      break;
    case '9':
      return 0x9;
      break;
    case 'A':
    case 'a':
      return 0xA;
      break;
    case 'B':
    case 'b':
      return 0xB;
      break;
    case 'C':
    case 'c':
      return 0xC;
      break;
    case 'D':
    case 'd':
      return 0xD;
      break;
    case 'E':
    case 'e':
      return 0xE;
      break;
    case 'F':
    case 'f':
      return 0xF;
      break;
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ted Klein Bergman
Solution 2 Community
Solution 3 Gimhani
Solution 4
Solution 5
Solution 6