I need to write a class (function) where I can change a hexadecimal to an integer and return the value. I have no idea where to begin. Can anyone help me?
I need to write a class (function) where I can change a hexadecimal to an integer and return the value. I have no idea where to begin. Can anyone help me?
First an integer stores its values in binary or twos complement if signed. You don't convert from hexadimal to integer. An integer is an integral number and hexadecimal is a base 16 representation of a value. Therefore its only important how you display it. Hexadecimal or Decimal or whatever. I think you want to call a funtion with an int and return an ascii string showing the value in hex. I could be all wet but this should get you started.
Code:#include "stdafx.h" #include <iostream> using namespace std; int main(int argc, char* argv[]) { int i = 100; char HexChar[80]; cout << "i hex = " << itoa( i, HexChar, 16 ) << endl; return 0; }
Code:#include <cstdio> class _hextodec { public: int operator()(const char* hex) const { int retval; sscanf(hex, "%x", &retval); return retval; } } hextodec; int main() { char* hex = "a8b1"; printf("%s = %d\n", hex, hextodec(hex)); return 0; }
- lmov
Thank you both so much, this has helped me out quite a bit