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?
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.
iss >> value
compares to true
or you can enable exceptions on the stream via iss.exceptions(std::ios::failbit | std::ios::badbit);
. –
Roseberry Try std::stoull
if you are using C++11 or greater.
This post may also be of help.
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.
std::istringstream
returns 0 if you provide a string that is not an integer. std::stoull
throws an std::invalid_argument
: Live Demo. –
Heddy iss >> value
compares to true
or you can enable exceptions on the stream via iss.exceptions(std::ios::failbit | std::ios::badbit);
. –
Roseberry 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";
}
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.
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
lexical_cast
jump in to be useful here? –
Dyche lexical_cast
is useful here or is this a request to OP to include an example? –
Peridotite 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. –
Peridotite .c_str()
, you can use the std::string
directly. That's probably also faster. –
Peridotite uint64_t to_uint64_t(const string& str)
{
static_assert(is_same_v<uint64_t, unsigned long> || is_same_v<uint64_t, unsigned long long>);
if constexpr (is_same_v<uint64_t, unsigned long>) // Linux 64
{
return stoul(str);
}
else // Linux 32 or Windows
{
return stoull(str);
}
}
I would rather use from_chars.
The advantages are:
- is way faster than
std::stoull
orstd::stringstream
. - no exception are thrown
- no local is used
- no dynamic memory allocation.
An example here
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;
}
}
© 2022 - 2024 — McMap. All rights reserved.
std::istringstream
returns 0 if you provide a string that is not an integer.std::stoull
throws anstd::invalid_argument
: Live Demo. – Heddy