-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumericConversion.java
More file actions
205 lines (187 loc) · 6.66 KB
/
NumericConversion.java
File metadata and controls
205 lines (187 loc) · 6.66 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
import java.util.Scanner;
public class NumericConversion {
public static short hexCharDecode(char digit){
char c = digit;
int a = 0;
if (c==0){
}
if (c == '1' || c == '2'|| c == '3'|| c == '4'|| c == '5'|| c == '6'|| c == '7'|| c == '8'|| c == '9'){
a = Integer.parseInt(String.valueOf(c));
}
//switch statements convert each letter
else{
switch (c){
case 'a':
case 'A':
a = 10;
break;
case 'b':
case 'B':
a = 11;
break;
case 'c':
case 'C':
a = 12;
break;
case 'd':
case 'D':
a = 13;
break;
case 'e':
case 'E':
a = 14;
break;
case 'f':
case 'F':
a = 15;
break;
}
}
//convert to short
short y = (short)a;
return y;
}
public static long hexStringDecode(String hex) {
int letterConverter = 0;
double total = 0;
//removes 0x or 0b from beginning
if (hex.substring(0,2).equalsIgnoreCase("0x")||hex.substring(0,2).equalsIgnoreCase("0b") ){
hex = hex.substring(2,hex.length());
}
for (int i = 0; i < hex.length(); i++){
char c = hex.charAt(i);
int a;
int x = (hex.length()-1-i);
//every letter put through hexchardecode
a = hexCharDecode(c);
double f = (a*(Math.pow(16, x)));
int g = (int)f;
total = f+total;
letterConverter++;
}
long finalTotal = (long)total;
return finalTotal;
}
public static short binaryStringDecode(String binary) {
int letterConverter = 0;
double total = 0;
//removes 0x or 0b from beginning
if (binary.substring(0,2).equalsIgnoreCase("0x")||binary.substring(0,2).equalsIgnoreCase("0b") ){
binary = binary.substring(2,binary.length());
}
//convert binary. Since only 1 and 2, do not need seperate class to convert each letter
for (int i = 0; i < binary.length(); i++){
char c = binary.charAt(i);
int a = Integer.parseInt(String.valueOf(c));
int x = (binary.length()-1-i);
double f = (a*(Math.pow(2, x)));
int g = (int)f;
total = f+total;
letterConverter++;
}
short finalTotal = (short)total;
return finalTotal;
}
public static String binaryToHex (String binary){
String hex="";
//makes sure number of characters is divisible by 4
while (!(binary.length()%4==0)){
binary = 0+binary;
}
//each 4 letters are converted to hex
for (int i = 0; i <= binary.length(); i++){
if (i%4==0&&!(i==0)){
String fourbit = binary.substring(i-4,i);
if (fourbit.equalsIgnoreCase("0000")){
hex = hex+0;
}
else if (fourbit.equalsIgnoreCase("0001")){
hex = hex+1;
}
else if (fourbit.equalsIgnoreCase("0010")){
hex = hex+2;
}
else if (fourbit.equalsIgnoreCase("0011")){
hex = hex+3;
}
else if (fourbit.equalsIgnoreCase("0100")){
hex = hex+4;
}
else if (fourbit.equalsIgnoreCase("0101")){
hex = hex+5;
}
else if (fourbit.equalsIgnoreCase("0110")){
hex = hex+6;
}
else if (fourbit.equalsIgnoreCase("0111")){
hex = hex+7;
}
else if (fourbit.equalsIgnoreCase("1000")){
hex = hex+8;
}
else if (fourbit.equalsIgnoreCase("1001")){
hex = hex+9;
}
else if (fourbit.equalsIgnoreCase("1010")){
hex = hex+"A";
}
else if (fourbit.equalsIgnoreCase("1011")){
hex = hex+"B";
}
else if (fourbit.equalsIgnoreCase("1100")){
hex = hex+"C";
}
else if (fourbit.equalsIgnoreCase("1101")){
hex = hex+"D";
}
else if (fourbit.equalsIgnoreCase("1110")){
hex = hex+"E";
}
else if (fourbit.equalsIgnoreCase("1111")){
hex = hex+"F";
}
}
}
return hex;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean ProgramRunning = true;
String operation = "";
String value = "";
long result;
//print menu with while loop
while (ProgramRunning) {
System.out.println("Decoding Menu ------------- " +
"\n1. Decode hexadecimal " +
"\n2. Decode binary " +
"\n3. Convert binary to hexadecimal " +
"\n4. Quit ");
System.out.println("Please enter an option: ");
operation = in.next();
if (!operation.equals("4")){
System.out.println("Please enter the numeric string to convert:");
value = in.next();
}
//for each case, prints needed output
switch (operation) {
case "4":
ProgramRunning = false;
System.out.println("Goodbye!");
break;
case "1":
result = hexStringDecode(value);
System.out.println("Result: "+result);
break;
case "2":
result = binaryStringDecode(value);
System.out.println("Result: "+result);
break;
case "3":
String result2 = binaryToHex(value);
System.out.println("Result: "+result2);
break;
}
}
}
}