-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryConverter.java
More file actions
240 lines (231 loc) · 3.94 KB
/
BinaryConverter.java
File metadata and controls
240 lines (231 loc) · 3.94 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
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
public class BinaryConverter
{
public static void main (String[] args) {
int dec = 0;
int con_cho = 0;
int binary = 0;
int decimal = 0;
int bit_8 = 0;
int bit_7 = 0;
int bit_6 = 0;
int bit_5 = 0;
int bit_4 = 0;
int bit_3 = 0;
int bit_2 = 0;
int bit_1 = 0;
int bit2 = 0;
String bit = "";
String dec_2 = "";
char quit = ' ';
boolean again = true;
System.out.println ("how do you want to convert?:");
System.out.println ();
System.out.println ("for: Binary to decimal: Press '1'");
System.out.println ("for: Decimal to Binary: Press '2'");
con_cho = SavitchIn.readLineInt();
if (con_cho == 1)
{
do
{
System.out.println ("Enter a 8-Bit number:");
bit = SavitchIn.readLine()
//128
if (bit.charAt(0) == ('1'))
{
dec+=128;
}
else
{
dec+=0;
}
//64
if (bit.charAt(1) == ('1'))
{
dec+=64;
}
else
{
dec+=0;
}
//32
if (bit.charAt(2) == ('1'))
{
dec+=32;
}
else
{
dec+=0;
}
//16
if (bit.charAt(3) == ('1'))
{
dec+=16;
}
else
{
dec+=0;
}
//8
if (bit.charAt(4) == ('1'))
{
dec+=8;
}
else
{
dec+=0;
}
//4
if (bit.charAt(5) == ('1'))
{
dec+=4;
}
else
{
dec+=0;
}
//2
if (bit.charAt(6) == ('1'))
{
dec+=2;
}
else
{
dec+=0;
}
//1
if (bit.charAt(7) == ('1'))
{
dec+=1;
}
else
{
dec+=0;
}
System.out.println ();
System.out.println ("Binary: " +bit);
System.out.println ("Decimal: "+dec);
System.out.println ();
dec = 0;
bit = "";
binary++;
System.out.println ("Binaries converted:"+binary);
System.out.println ("Decimals converted:"+decimal);
System.out.println("Convert another Binary?");
quit = SavitchIn.readNonwhiteChar();
if (quit == 'Y' || quit =='y')
{
again = true;
}
if (quit == 'N' || quit == 'n')
{
again = false;
}
}while (again == true);
System.out.println ("how do you want to convert?:");
System.out.println ();
System.out.println ("for: Binary to decimal: Press '1'");
System.out.println ("for: Decimal to Binary: Press '2'");
con_cho = SavitchIn.readLineInt();
}
if (con_cho == 2)
{
do
{
System.out.println ("Enter a number less than or equal to 255");
dec = SavitchIn.readLineInt();
while (dec > 255)
{
System.out.println ("Try Again");
dec = SavitchIn.readLineInt();
}
if (dec > 128 && dec-128 >= 0)
{
bit_8 = 1;
}
else
{
bit_8 =0;
}
if (dec > 64 && dec-64 >= 0)
{
bit_7 = 1;
}
else
{
bit_7 =0;
}
if (dec > 32 && dec-32 >= 0)
{
bit_6 = 1;
}
else
{
bit_6 =0;
}
if (dec > 16 && dec-16 >= 0)
{
bit_5 = 1;
}
else
{
bit_5 =0;
}
if (dec > 8 && dec-8 >= 0)
{
bit_4 = 1;
}
else
{
bit_4 =0;
}
if (dec > 4 && dec-4 >= 0)
{
bit_3 = 1;
}
else
{
bit_3 =0;
}
if (dec > 2 && dec-2 >= 0)
{
bit_2 = 1;
}
else
{
bit_2 =0;
}
if (dec > 1 && dec-1 >= 0)
{
bit_1 = 1;
}
else
{
bit_1 =0;
}
System.out.println ("Decimal: " +dec);
System.out.println ("Binary: "+bit_8+""+bit_7+""+bit_6+""+bit_5+" "+bit_4+""+bit_3+""+bit_2+""+bit_1);
System.out.println ();
dec = 0;
bit = "";
decimal++;
System.out.println ("Binaries converted:"+binary);
System.out.println ("Decimals converted:"+decimal);
System.out.println("Convert another decimal?");
quit = SavitchIn.readNonwhiteChar();
if (quit == 'Y' || quit =='y')
{
again = true;
}
if (quit == 'N' || quit == 'n')
{
again = false;
}
}while (again == true);
System.out.println ("how do you want to convert?:");
System.out.println ();
System.out.println ("for: Binary to decimal: Press '1'");
System.out.println ("for: Decimal to Binary: Press '2'");
}
con_cho = SavitchIn.readLineInt();
}
}