954,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

hex2int problems

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++

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

I just tried the following function but again it doesn't work. Does anybody know why hex's with high decimal values like bb76e739 just won't convert? I know it should equal 3145131833 but this function just like every other one shows different.

int hex2int (std::string str)
{
int num;
std::istringstream i(str);
i >> std::hex >> num;
return num;
}

Can anybody suggest a function that will convert with 100% accuracy as this is just crazy because when I convert to from dec to hex 3145131833=bb76e739 but when I try to reverse the process it does not reverse the sum. Any ideas?

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

sigh. Its practically the same concept as before, when I gave you
toHex() function. This time try to learn whats going on, please.

#include <iostream>
#include <sstream>
#include <string>

using namespace std;
template<typename Type>
string toHex(const Type& value, bool showBase = true){
	stringstream strm;
	if(showBase)
		strm << showbase;
	strm  <<  hex << value;
	string to_hex;
	if(!(strm >>  to_hex)) throw std::exception("Conversion to hex failed!\n");
	return to_hex;
}
typedef __int64 int64;

int64 hexToInt64(const string hexStr){
	stringstream strm;
	strm << hex << hexStr;
	int64 value = 0;
	if(!(strm >> value)) throw std::exception("Conversion to int64 failed!\n");
	return value;
}
int main(){
	cout << hexToInt64(toHex(3145131833)) << endl;	
	cout << toHex( hexToInt64("bb76e739")) << endl;
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

WOW! Now I see it. The int was so big that it exceeded it's 32bit signed int. Therefore needed converting to 64bit then back to 32bit unsigned long int. Thanks for that. Now I can crack the second layer of SHA1 hopefully with no problem. THANKS!!
sigh. Its practically the same concept as before, when I gave you
toHex() function. This time try to learn whats going on, please.

#include <iostream>
#include <sstream>
#include <string>

using namespace std;
template<typename Type>
string toHex(const Type& value, bool showBase = true){
	stringstream strm;
	if(showBase)
		strm << showbase;
	strm  <<  hex << value;
	string to_hex;
	if(!(strm >>  to_hex)) throw std::exception("Conversion to hex failed!\n");
	return to_hex;
}
typedef __int64 int64;

int64 hexToInt64(const string hexStr){
	stringstream strm;
	strm << hex << hexStr;
	int64 value = 0;
	if(!(strm >> value)) throw std::exception("Conversion to int64 failed!\n");
	return value;
}
int main(){
	cout << hexToInt64(toHex(3145131833)) << endl;	
	cout << toHex( hexToInt64("bb76e739")) << endl;
}
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

Come to think of it. Make a function for everything.

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

//supports hex, dec and octal
//base can only equal decimal(10),octal(8),or hex(16)
template<typename ReturnType, typename InputType>
void convertBases(const InputType& src, ReturnType& dest,const int base,bool showBase = true)
{
	stringstream stream;
	
	if(showBase) 
		stream << showbase;	

	switch(base)
	{
	case 10: stream << dec;  break; /* dec by default but...*/ 
	case 16: stream << hex;  break;
	case 8:  stream << oct; break;
	default: throw std::exception("Base not supported"); break;
	}
	
	stream << src;

	if(!(stream >> dest) ){
		string errMsg = "Conversion failed\n";
		errMsg +=  "Tried to convert \"" + string(typeid(src).name());
		errMsg +=  "\" into \"" + string(typeid(dest).name()) + "\"";
		throw std::exception(errMsg.c_str());
	}	
}
int main(){
	const int decValue = 15;

	enum{ HEX = 16, DEC = 10, OCT = 8 };

	string hexValue;
	string octalValue;
	string deciValue;

	try{
		convertBases(decValue,hexValue,HEX);
		convertBases<string>(decValue,octalValue,OCT);
		convertBases<string>(decValue,deciValue,DEC);
	}catch(std::exception& e) { 
		cout << e.what() << endl;
		return -1; 
	}

	cout <<"For " << decValue << " : \n";
	cout << "hexValue = " << hexValue << endl;
	cout << "octalValue = " << octalValue << endl;
	cout << "decValue = " << deciValue << endl;
	cout << endl;

	return 0;
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You