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?
6 Answers
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.
-
2Well, c++11 can be considered the current standard since several years now. – πάντα ῥεῖ Feb 21 '17 at 01:11
-
1It 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. – Gambit Feb 21 '17 at 01:13
-
1Surely **THE** standard is C++14? – Tristan Brindle Feb 21 '17 at 01:14
-
1@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. – Cauchy Feb 21 '17 at 01:14
-
@Gambit _"but not **THE** standard"_ It's the current standard, I didn't say more. I even upvoted your answer dude! – πάντα ῥεῖ Feb 21 '17 at 01:14
-
2@Cauchy it must be at least 64 bits, therefore it can hold a 64-bit integer – M.M Feb 21 '17 at 01:19
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.

- 9,146
- 4
- 29
- 50

- 1
- 13
- 116
- 190
-
1As 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](http://coliru.stacked-crooked.com/a/ae033fe52c6cc876). – davidvandebunte Oct 18 '18 at 21:42
-
@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);`. – Darklighter Sep 23 '19 at 13:46
-
actually std::istringstream will not return 0 if the string starts with digits. It will return the digits till the first non digit char. – Avizipi Oct 12 '21 at 13:22
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";
}

- 1,318
- 13
- 23
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.

- 502
- 6
- 16
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

- 763
- 5
- 16
-
-
@BaummitAugen Hhhhhm, how does _`lexical_cast`_ jump in to be useful here? – πάντα ῥεῖ Feb 21 '17 at 02:00
-
@πάνταῥεῖ Are you asking me why `lexical_cast` is useful here or is this a request to OP to include an example? – Baum mit Augen Feb 21 '17 at 02:04
-
-
1@πάνταῥεῖ 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. – Baum mit Augen Feb 21 '17 at 02:10
-
@πάνταῥεῖ 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. – Vada Poché Feb 21 '17 at 02:32
-
1@Vada That doesn't hinder you to improve your answer according to be self contained and showing a good and reusable code example. – πάντα ῥεῖ Feb 21 '17 at 02:35
-
1No need for the `.c_str()`, you can use the `std::string` directly. That's probably also faster. – Baum mit Augen Feb 21 '17 at 07:39
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;
}
}
-
4I 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. – ChemiCalChems Feb 23 '20 at 09:46
-
Ca you please explain more about what are you trying to achieve instead just posting code ? – Kyrol Feb 23 '20 at 12:47
-
1@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 Feb 25 '20 at 09:11
-
1@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. – Hiloliddin Jaloliddinzoda Feb 25 '20 at 09:14
-
@HiloliddinJaloliddinzoda Thanks for the explanation and the comments. +1 because, even if it's not C++, it's still a nice solution. – Kyrol Mar 03 '20 at 22:32