Hi and I have a hex to int function but it isn't converting the hex to integers properly. Below is the code an example is the hex bb76e739 which should = 3145131833 but with the function it equals 2147483647. I heard sprintf or something like that can convert the hex to int and assign it to a variable but have no code for it. Below is my current function which doesn't always work.
long hex2int(const std::string& hexStr) {
char *offset;
if (hexStr.length( ) > 2) {
if (hexStr[0] == '0' && hexStr[1] == 'x') {
return strtol(hexStr.c_str( ), &offset, 0);
}else{
std::string str="0x";
str.append(hexStr);
return strtol(str.c_str( ), &offset, 0);
}
}
}
Can anybody suggest a better function where the input is a std::string because this is causing all sorts of troubles?
Edit: I'm using C++ with VC++