conversion programm
im having trouble with the hex 2 dec conversion ... all the others work
heres my code
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
// bianary converter sample.cpp : Defines the entry point for the console application.
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
void decToBin( int, int );
void convert (int);
void change (string, int);
int main()
{
string hexiNum = "";
int num = 0;
int num2 = 0;
int decimalVal = 0;
int total = 0;
// Creating a variable of type integer
int decimal;
// message to the user to input his age
cout << "Enter a Decimal Integer to convert it to binary: ";
cin >> decimal; // save it to the varible
cout << "\nIn Binary is: ";
decToBin( decimal, 2 ); // calling the function to convert to binary
cout << endl; // output a newline
cout <<"in hex: " << hex << decimal << endl;
cout << "_____________________________________________________________________" << endl;
cout << endl;
// binary to decimal
cout << "Enter a binary integer (0 to quit): ";
cin >> num2;
int temp = num2;
cout << "\nconverted to decimal is : ";
convert (num2);
cout << endl;
cout << "_____________________________________________________________________" << endl;
cout << endl;
//hex to decimal and bianary
cout << "Enter a hexadecimal: ";
cin >> hexiNum;
cout << hexiNum << " In decimal form is: " << total << endl;
change(hexiNum, total);
system ("pause");
return 0;
} // end main
// a recursive function to find the binary of an integer
void decToBin(int num, int base)
{
if (num > 0)
{
decToBin(num/base, base);
cout<< num % base;
}
} // end function decToBin
void convert (int binary)
{
int hold = 1;
int final = 0;
while (binary != 0)
{
if (binary % 2 == 1)
{
binary = (binary-1)/10;
final += hold;
}
else if (binary % 2 == 0)
{
binary = binary/10;
}
hold *= 2;
}
cout << dec << final << endl;
cout<< "In hex: " << hex << final << endl;
}
void change (string hexiNum, int total)
{
char x = ' ';
char x2 = ' ';
int val = 0;
int numeric = 0;
int number = 0;
int number2 = 0;
int numeric2 = 0;
if (isalpha(hexiNum.at(0)))
{
x = hexiNum.at(0);
toupper('x');
if (x == 'A')
{
number = 160;
}
else if (x == 'B')
{
number = 176;
}
else if (x == 'C')
{
number = 192;
}
else if (x == 'D')
{
number = 208;
}
else if (x == 'E')
{
number = 224;
}
else if (x == 'F')
{
number = 240;
}
}
else if (isalnum(hexiNum.at(0)))
{
numeric = hexiNum.at(0);
if (numeric == 0)
{
number = 0;
}
else if (numeric == 1)
{
number = 16;
}
else if (numeric == 2)
{
number = 32;
}
else if (numeric == 3)
{
number = 48;
}
else if (numeric == 4)
{
number = 64;
}
else if (numeric == 5)
{
number = 80;
}
else if (numeric == 6)
{
number = 96;
}
else if (numeric == 7)
{
number = 112;
}
else if (numeric == 8)
{
number = 128;
}
else if (numeric == 9)
{
number = 144;
}
}
if (isalpha(hexiNum.at(1)))
{
x2 = hexiNum.at(0);
toupper('x2');
if (x2 == 'A')
{
number2 = 160;
}
else if (x2 == 'B')
{
number2 = 176;
}
else if (x2 == 'C')
{
number2 = 192;
}
else if (x2 == 'D')
{
number2 = 208;
}
else if (x2 == 'E')
{
number2 = 224;
}
else if (x2 == 'F')
{
number2 = 240;
}
}
else if (isalnum(hexiNum.at(1)))
{
number2 = hexiNum.at(1);
}
cout << x << endl;
cout << x2 << endl;
cout << number << endl;
cout << number2 << endl;
total = (number + number2);
}
| |
Topic archived. No new replies allowed.