943,097 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1036
  • C++ RSS
Feb 9th, 2010
0

hex2int problems

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  1. long hex2int(const std::string& hexStr) {
  2. char *offset;
  3.  
  4. if (hexStr.length( ) > 2) {
  5. if (hexStr[0] == '0' && hexStr[1] == 'x') {
  6. return strtol(hexStr.c_str( ), &offset, 0);
  7. }else{
  8. std::string str="0x";
  9. str.append(hexStr);
  10. return strtol(str.c_str( ), &offset, 0);
  11. }
  12. }
  13. }
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++
Last edited by cwarn23; Feb 9th, 2010 at 12:12 am.
Similar Threads
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,003 posts
since Sep 2007
Feb 9th, 2010
0
Re: hex2int problems
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.
C++ Syntax (Toggle Plain Text)
  1. int hex2int (std::string str)
  2. {
  3. int num;
  4. std::istringstream i(str);
  5. i >> std::hex >> num;
  6. return num;
  7. }
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?
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,003 posts
since Sep 2007
Feb 9th, 2010
2
Re: hex2int problems
sigh. Its practically the same concept as before, when I gave you
toHex() function. This time try to learn whats going on, please.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6. template<typename Type>
  7. string toHex(const Type& value, bool showBase = true){
  8. stringstream strm;
  9. if(showBase)
  10. strm << showbase;
  11. strm << hex << value;
  12. string to_hex;
  13. if(!(strm >> to_hex)) throw std::exception("Conversion to hex failed!\n");
  14. return to_hex;
  15. }
  16. typedef __int64 int64;
  17.  
  18. int64 hexToInt64(const string hexStr){
  19. stringstream strm;
  20. strm << hex << hexStr;
  21. int64 value = 0;
  22. if(!(strm >> value)) throw std::exception("Conversion to int64 failed!\n");
  23. return value;
  24. }
  25. int main(){
  26. cout << hexToInt64(toHex(3145131833)) << endl;
  27. cout << toHex( hexToInt64("bb76e739")) << endl;
  28. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,859 posts
since Dec 2008
Feb 9th, 2010
0
Re: hex2int problems
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.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6. template<typename Type>
  7. string toHex(const Type& value, bool showBase = true){
  8. stringstream strm;
  9. if(showBase)
  10. strm << showbase;
  11. strm << hex << value;
  12. string to_hex;
  13. if(!(strm >> to_hex)) throw std::exception("Conversion to hex failed!\n");
  14. return to_hex;
  15. }
  16. typedef __int64 int64;
  17.  
  18. int64 hexToInt64(const string hexStr){
  19. stringstream strm;
  20. strm << hex << hexStr;
  21. int64 value = 0;
  22. if(!(strm >> value)) throw std::exception("Conversion to int64 failed!\n");
  23. return value;
  24. }
  25. int main(){
  26. cout << hexToInt64(toHex(3145131833)) << endl;
  27. cout << toHex( hexToInt64("bb76e739")) << endl;
  28. }
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,003 posts
since Sep 2007
Feb 9th, 2010
1
Re: hex2int problems
Come to think of it. Make a function for everything.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. //supports hex, dec and octal
  8. //base can only equal decimal(10),octal(8),or hex(16)
  9. template<typename ReturnType, typename InputType>
  10. void convertBases(const InputType& src, ReturnType& dest,const int base,bool showBase = true)
  11. {
  12. stringstream stream;
  13.  
  14. if(showBase)
  15. stream << showbase;
  16.  
  17. switch(base)
  18. {
  19. case 10: stream << dec; break; /* dec by default but...*/
  20. case 16: stream << hex; break;
  21. case 8: stream << oct; break;
  22. default: throw std::exception("Base not supported"); break;
  23. }
  24.  
  25. stream << src;
  26.  
  27. if(!(stream >> dest) ){
  28. string errMsg = "Conversion failed\n";
  29. errMsg += "Tried to convert \"" + string(typeid(src).name());
  30. errMsg += "\" into \"" + string(typeid(dest).name()) + "\"";
  31. throw std::exception(errMsg.c_str());
  32. }
  33. }
  34. int main(){
  35. const int decValue = 15;
  36.  
  37. enum{ HEX = 16, DEC = 10, OCT = 8 };
  38.  
  39. string hexValue;
  40. string octalValue;
  41. string deciValue;
  42.  
  43. try{
  44. convertBases(decValue,hexValue,HEX);
  45. convertBases<string>(decValue,octalValue,OCT);
  46. convertBases<string>(decValue,deciValue,DEC);
  47. }catch(std::exception& e) {
  48. cout << e.what() << endl;
  49. return -1;
  50. }
  51.  
  52. cout <<"For " << decValue << " : \n";
  53. cout << "hexValue = " << hexValue << endl;
  54. cout << "octalValue = " << octalValue << endl;
  55. cout << "decValue = " << deciValue << endl;
  56. cout << endl;
  57.  
  58. return 0;
  59. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,859 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dynamic Memory 2d Array
Next Thread in C++ Forum Timeline: Event Handling





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC