#include <iostream>
#include <string>
#include <cmath>
usingnamespace std;
int parseHex(const string &hexString)
{
int i = 0;
int sum = 0;
int powercount = 0;
int temp = 0;
int count = hexString.length(); // Finds length of string
for(i = count-1; i>=0; i--) // Reads string from right to left.
{
switch(hexString[i])
{
// The switch statement defines each HEX variable.
case'A': temp = 10;break;
case'B': temp = 11;break;
case'C': temp = 12;break;
case'D': temp = 13;break;
case'E': temp = 14;break;
case'F': temp = 15;break;
default:
/* Referenced to ASCII chart - The ascii code inputs of 0 - 9 is 48 - 57.
Therefore, when hexString[i] is anywhere between 48 - 57, taking that number and subtracting 48 will display the right result.*/
if(int(hexString[i]) >= 48 && int(hexString[i]) <= 57)
temp = hexString[i] - 48;
else
cout<<"Error: Lowercase letters will not be accepted."<<endl; // Hex numbers should be capitalized.
}
sum += temp*pow(double(16),double(powercount));
powercount++;
}
return sum;
}
int main()
{
string hexString;
cout<<"Enter in a hex number: "<<endl;
getline(cin,hexString); // input string
cout<<"Decimal Form: "<<parseHex(hexString)<<"\n\n"; // prints converted decimal answer.
system("pause");
}
So far I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <cmath>
#include <string>
usingnamespace std;
class HexConvert
{
public:
HexConvert(); // Default Constructor
HexConvert(string); // Constructor with an object on it
string getHex(); // Accessor function
int parseHex(); // Function that will
~HexConvert(); // Destructor
private:
string hexString; // Data field
};