Hex to Dec converter
Sep 30, 2013 at 6:14am UTC
I've written this program which repeatedly offers the choice to convert unsigned Hex to Dec or unsigned Dec to Hex (Teacher wants us do not use the scanf and printf methods with the descriptors %d and %x).
I don't know why it asks to enter hex in either choices?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int hexToDec(const string &hex);
int hexCharToDec(char ch);
void DecToHex(int decNum);
int main()
{
cout << "Select 1 for Hex to Dec or 2 for Dec to Hex: " ;
int x = 0;
cin >> x;
if ( x = 1 ){
cout << "Enter hex number: " ;
string hex;
cin >> hex;
unsigned decimal = -1;
decimal = hexToDec(hex);
if (decimal != -1)
cout << "The decimal is " << decimal << endl;
}
if (x=2){
unsigned decNum = 0;
cout << "Enter Decimal Number: " ;
cin >> decNum;
cin.clear();
cin.sync();
cout << "Hexadecimal N.: " ;
if ( decNum )DecToHex(decNum);
else cout << "0" ;
cout << endl << endl;
}
cin.get();
cin.get();
return 0;
}
int hexToDec(const string &hex)
{
int decimalValue = 0;
for (int index = 0; index < hex.size(); index++)
{
try
{
char ch = toupper(hex[index]);
if (!(ch >= 'A' && ch <= 'F' || ch >= '0' && ch <= '9' ))
{
throw runtime_error("is not a hex string" );
}
else
decimalValue = decimalValue * 16 + hexCharToDec(hex[index]);
}
catch (runtime_error e)
{
cout << e.what();
return -1;
}
}
return decimalValue;
}
int hexCharToDec(char ch)
{
ch = toupper(ch);
if (ch >= 'A' && ch <= 'F' )
return 10 + ch - 'A' ;
else
return ch - '0' ;
}
void DecToHex(int decNum)
{
int x = decNum;
if (decNum / 16 > 0)
{
x = decNum%16;
decNum = decNum /16;
DecToHex(decNum);
}
if ( x > 9 )
{
switch (x)
{
case 10 : cout << "A" ; break ;
case 11 : cout << "B" ; break ;
case 12 : cout << "C" ; break ;
case 13 : cout << "D" ; break ;
case 14 : cout << "E" ; break ;
case 15 : cout << "F" ; break ;
}
}
else
{
cout << x;
}
}
Sep 30, 2013 at 12:26pm UTC
@behzad1355
I don't know why it asks to enter hex in either choices?
For one simple reason. You're using the single equal sign, which is assigning 1 to x, instead of using the double equal sign, which is to check if a variable is equal to what you are looking for. So, just change the single =, to a ==, and you're done.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
if ( x = 1 ){ // Change to a double ==
cout << "Enter hex number: " ;
string hex;
cin >> hex;
unsigned decimal = -1;
decimal = hexToDec(hex);
if (decimal != -1)
cout << "The decimal is " << decimal << endl;
}
if (x=2){ // Change to a double ==, also
unsigned decNum = 0;
cout << "Enter Decimal Number: " ;
cin >> decNum;
cin.clear();
cin.sync();
cout << "Hexadecimal N.: " ;
if ( decNum )DecToHex(decNum);
else cout << "0" ;
cout << endl << endl;
}
Sep 30, 2013 at 11:42pm UTC
Thank you,
Last edited on Oct 2, 2013 at 1:49am UTC
Oct 2, 2013 at 1:49am UTC
how to change the program to convert signed Hex to Dec or signed Dec to Hex?
Oct 2, 2013 at 2:54am UTC
@behzad1355
how to change the program to convert signed Hex to Dec or signed Dec to Hex?
This will do it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
// Hex Convertor.cpp : main project file.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int hexToDec(const string &hex);
int hexCharToDec(char ch);
void DecToHex(int decNum);
int main()
{
cout << "Select 1 for Hex to Dec or 2 for Dec to Hex: " ;
int x = 0;
cin >> x;
if ( x == 1 )
{
cout << "Enter hex number: " ;
string hex;
cin >> hex;
if (hex[0] == '-' )
{
std::string Signed_Hex = hex;
unsigned char chTest = std::stoi( Signed_Hex, nullptr , 16 );
char chTest2 = *reinterpret_cast <char *>(&chTest);
std::cout << Signed_Hex << ": " << static_cast <int >(chTest2) << std::endl;
}
else
{
unsigned decimal;
decimal = hexToDec(hex);
if (decimal != -1)
cout << "The decimal is " << decimal << endl;
}
}
if (x==2)
{
int decNum = 0;
cout << "Enter Decimal Number: " ;
cin >> decNum;
cin.clear();
cin.sync();
cout << "Hexadecimal N.: " ;
if ( decNum < 0 )
cout << "-" ;
DecToHex(decNum);
cout << endl << endl;
}
cin.get();
cin.get();
return 0;
}
int hexToDec(const string &hex)
{
int decimalValue = 0;
for (int index = 0; index < hex.size(); index++)
{
try
{
char ch = toupper(hex[index]);
if (!(ch >= 'A' && ch <= 'F' || ch >= '0' && ch <= '9' ))
{
throw runtime_error("is not a hex string" );
}
else
decimalValue = decimalValue * 16 + hexCharToDec(hex[index]);
}
catch (runtime_error e)
{
cout << e.what();
return -1;
}
}
return decimalValue;
}
int hexCharToDec(char ch)
{
ch = toupper(ch);
if (ch >= 'A' && ch <= 'F' )
return 10 + ch - 'A' ;
else
return ch - '0' ;
}
void DecToHex(int decNum)
{
decNum = abs(decNum);
int x = decNum;
if (decNum / 16 > 0)
{
x = decNum%16;
decNum = decNum /16;
DecToHex(decNum);
}
if ( x > 9 )
{
switch (x)
{
case 10 : cout << "A" ; break ;
case 11 : cout << "B" ; break ;
case 12 : cout << "C" ; break ;
case 13 : cout << "D" ; break ;
case 14 : cout << "E" ; break ;
case 15 : cout << "F" ; break ;
}
}
else
{
cout << x;
}
}
Topic archived. No new replies allowed.