-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytoString.cpp
More file actions
171 lines (142 loc) · 3.79 KB
/
Copy pathbinarytoString.cpp
File metadata and controls
171 lines (142 loc) · 3.79 KB
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
#include <bits/stdc++.h>
using namespace std;
void decimaltoString(int n, vector<int> sum) {
for (int i = 0; i < n; i++) {
char character = sum[i];
cout << character;
}
}
vector<int> binarytoDecimal(int n, vector<vector<int>> binary) {
vector<int> sum(n);
for (int i = 0; i < n; i++) {
int m=1;
for (int j = 0; j<8; j++) {
int transfer = 128/m;
m = m*2;
sum[i] += binary[i][j] * transfer;
}
}
return sum;
}
vector<vector<int>> stringtoVector(int n, string str) {
vector<vector<int>> binary(n, vector<int>(8));
int current_index = 0;
for (int i=0; i<n; i++) {
for (int j=0; j<8;j++) {
if(current_index < str.length() && !(str[current_index]=='0' || str[current_index]=='1')) {
current_index++;
}
if(current_index < str.length()) {
binary[i][j] = str[current_index] - '0';
current_index++;
}
else{
cout << "Not enough!";
break;
}
}
}
return binary;
}
int number_of_digits(string str) {
int count = 0;
for (char c: str) {
if( c == '0' || c == '1') {
count++;
}
}
return count;
}
bool check_digits(string str)
{
for (int i = 0; i < str.length(); i++)
{
if (isdigit(str[i]) == false)
{
return false;
}
}
return true;
}
int main() {
while(1)
{
cout << "Binary or Decimal? (Enter \"End\" to stop)" << endl << "Enter your choice: ";
string choice;
getline(cin,choice);
if (choice == "Binary") {
cout << "Binary Input: ";
string str;
getline(cin, str);
int digits = number_of_digits(str);
int n = 0;
if(digits >= 8) {
n = digits/8;
}
else {
cout << "Please enter in the right format!" << endl << endl;
continue;
}
for (int i = 0; i < str.length(); i++) {
if(!(str[i]=='1'||str[i]=='0'||str[i]==' ')) {
cout << "Please enter in the right format!" << endl << endl;
continue;
}
}
if(digits%8!=0) {
cout << "Please enter in the right format!" << endl << endl;
continue;
}
cout << "Numbers of characters: " << n << endl;
vector<vector<int>> binaryVector = stringtoVector(n, str);
for (int i=0; i<n; i++) {
for (int j=0; j<8; j++) {
cout << binaryVector[i][j];
}
cout << " ";
}
cout << endl;
vector<int> decimal = binarytoDecimal(n, binaryVector);
for (int i = 0; i< n; i++) {
cout << decimal[i] << " ";
}
cout << endl << "Text: ";
decimaltoString(n, decimal);
cout << endl << endl;
continue;
}
else if (choice == "Decimal")
{
cout << "Decimal input: ";
string str;
getline(cin, str);
vector <int> decimal;
istringstream user_iss(str);
int user_number;
while (user_iss >> user_number) {
decimal.push_back(user_number);
}
for (int i = 0; i < decimal.size(); i++) {
if (decimal[i] < 128 && decimal[i] >=0) {
cout << "Text: ";
decimaltoString(decimal.size(), decimal);
cout << endl << endl;
break;
}
else {
cout << "Please enter numbers from 0 to 127!" << endl << endl;
break;
}
}
continue;
}
else if (choice == "End") {
return 0;
}
else {
cout << "Please enter either Binary or Decimal!" << endl << endl;
continue;
}
}
return 0;
}