-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbinBaseConv.cpp
66 lines (56 loc) · 1.19 KB
/
binBaseConv.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
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int bin2Dec(string binNum);
string dec2Bin(int decNum);
int main() {
int choice;
int decimalNum;
string binarynum;
cout << "Type of conversion [1 for binary to decimal, 2 for decimal to binary]: ";
scanf("%d", &choice);
switch (choice) {
case 1:
cout << "Enter binary number: ";
cin.ignore();
std::getline(cin, binarynum);
cout << "decimal: " << bin2Dec(binarynum) << endl;
break;
case 2:
cout << "Enter deciaml number: ";
scanf("%d", &decimalNum);
cout << "binary: " << dec2Bin(decimalNum) << endl;
break;
default:
cerr << "Invalid conversion type" << endl;
}
return 0;
}
int bin2Dec(string binNum) {
int length = binNum.size() - 1;
int dec = 0;
int exp = 0;
int i;
for (i = length; i >= 0; i--, exp++) {
dec += (binNum.at(i) % 48) << exp;
}
return dec;
}
string dec2Bin(int decNum) {
string binNum = "";
string temp = "";
ostringstream str;
while (decNum > 0) {
str << (decNum % 2);
decNum = int(decNum >> 1);
}
temp += str.str();
int i;
for (i = temp.size() - 1; i >= 0; i--) {
binNum += temp.at(i);
}
return binNum;
}