-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_7_bin_dec_hex_converter_1.cpp
248 lines (180 loc) · 4.82 KB
/
2_7_bin_dec_hex_converter_1.cpp
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
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <iostream>
#include <string>
/*
Dec hex bin conversion with strings
*/
using std::cin;
using std::cout;
using std::string;
using std::endl;
using std::getline;
string decimalToBinary(int dec);
string decimalToHex(int dec);
int binaryToDecimal(string bin);
string binaryToHex(string bin);
int hexToDecimal(string hex);
string hexToBinary(string hex);
int main()
{
int dec = 0;
cout << "Type in decimal number and convert it to binary and hexadecimal:\n";
cin >> dec;
cin.ignore();
string bin = decimalToBinary(dec);
cout << "The binary of " << dec << " is " << bin << endl;
string hex = decimalToHex(dec);
cout << "The hexadecimal of " << dec << " is " << hex << endl;
cout << "Type in binary number and convert it to decimal and hexadecimal:\n";
getline(cin, bin, '\n');
dec = binaryToDecimal(bin);
cout << "The decimal of " << bin << " is " << dec << endl;
hex = binaryToHex(bin);
cout << "The hexadecimal of " << bin << " is " << hex << endl;
cout << "Type in hexadecimal number and convert it to decimal and binary:\n";
getline(cin, hex, '\n');
dec = hexToDecimal(hex);
cout << "The decimal of " << hex << " is " << dec << endl;
bin = hexToBinary(hex);
cout << "The binary of " << hex << " is " << bin << endl;
cin.get();
return 0;
}
string decimalToBinary(int dec=0)
{
int temp = dec;
char digit = 0;
string binPart = "";
string bin = "";
do {
binPart = "0000";
for(int i = 3; i >= 0; i--)
{
digit = temp % 2 + '0';
binPart[i] = digit;
temp /= 2;
if(!temp) break;
}
bin = temp ? " " + binPart + bin: binPart + bin;
} while (temp);
return bin;
}
string decimalToHex(int dec=0)
{
int temp = dec;
char digit = 0;
string hex = "";
int rest = 0;
int i = 4; //Counting for digit space
do {
i--;
rest = temp % 16;
digit = rest < 10 ? rest + '0': rest + 'A' - 10;
hex = digit + hex;
temp /= 16;
if (temp && i == 0)
{
hex = " " + hex;
i=4;
}
} while (temp);
return hex;
}
int binaryToDecimal(string bin)
{
int dec = 0;
int digit = 0;
int length = bin.size();
for(int i = 0; i < length; i++)
{
if (bin[i] == ' ') continue;
digit = bin[i] - '0';
dec = dec*2 + digit;
}
return dec;
}
string binaryToHex(string bin)
{
string hex = "";
char digit = 0;
int length = bin.size()-1;
if (!length) hex = "0000";
int temp = 1;
int dec = 0;
int j = 4; //index to count nr of binary digits left (4 binary digit is one hex digit)
int k = 4; //hex index to control spaces
for(int i = length; i >=0; i--)
{
if (bin[i] == ' ') continue;
digit = bin[i];
if (digit == '0') {
temp *= 2;
}
if (digit == '1') {
dec += temp;
temp *= 2;
}
j--;
if (j == 0 || i == 0) {
int rest = dec % 16;
digit = rest < 10 ? rest + '0': rest + 'A' - 10;
j = 4;
dec = 0; temp = 1;
hex = digit + hex;
k--;
if (k==0 && i > 0)
{
hex = " " + hex;
k = 4;
}
}
}
return hex;
}
string hexToBinary(string hex)
{
string bin = "";
string binPart = "";
char ch = 0;
int length = hex.size();
if (!length) bin = "0000";
for (int i = 0; i < length; i++)
{
ch = hex[i];
if (ch == ' ') continue;
switch(ch)
{
case '0': binPart = "0000"; break;
case '1': binPart = "0001"; break;
case '2': binPart = "0010"; break;
case '3': binPart = "0011"; break;
case '4': binPart = "0100"; break;
case '5': binPart = "0101"; break;
case '6': binPart = "0110"; break;
case '7': binPart = "0111"; break;
case '8': binPart = "1000"; break;
case '9': binPart = "1001"; break;
case 'A': binPart = "1010"; break;
case 'B': binPart = "1011"; break;
case 'C': binPart = "1100"; break;
case 'D': binPart = "1101"; break;
case 'E': binPart = "1110"; break;
case 'F': binPart = "1111"; break;
default: binPart = "error"; break;
}
bin += i <= length ? binPart + " " : binPart;
}
return bin;
}
int hexToDecimal(string hex)
{
int dec = 0;
int digit = 0;
int length = hex.size();
for(int i = 0; i < length; i++)
{
if (hex[i] == ' ') continue;
digit = hex[i] < 'A' ? hex[i] - '0': hex[i] - 'A' + 10;
dec = dec*16 + digit;
}
return dec;
}