-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversions.java
More file actions
131 lines (114 loc) · 4.33 KB
/
Copy pathConversions.java
File metadata and controls
131 lines (114 loc) · 4.33 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
package Text_to_Binary;
public class Conversions {
// kicking off the whole program
public static void main(String[] args0) {
// checks to verify input was given at program start
if (args0.length <= 0 || args0.length > 2) {
System.out
.println("There was an error you did not give this program valid input. This program takes in a string and an optional argument of -v.");
System.exit(0);
}
if (args0.length == 2 && args0[1].equalsIgnoreCase("-v")) {
BinaryToDecimal(Verbose(args0[0]));
} else {
NotVerbose(args0[0]);
}
}
/**
* This word will be converted into decimal, then binary and all steps will
* be printed
*
* @param word
*/
private static String Verbose(String word) {
// this method is static since this is a very small program and OOP is
// unnecessary for the very small scope
String binary = "";
// this for loop will go over the entirety of the word and convert to
// binary and print each step on a separate line, before printing final
// results on last line
for (int i = 0; i <= word.length() - 1; i++) {
int letter = word.charAt(i);
String tempBinary = "";
System.out.println("\n");
System.out.println("The letter " + word.charAt(i) + " is " + letter
+ " in decimal based on ASCII values.\n");
while (letter > 0) {
System.out.println(letter + "/2 = " + (letter / 2)
+ " Remainder " + letter % 2);
tempBinary += letter % 2;
letter = letter / 2;
}
// reversing the tempBinary string because for some reason that is how this method of conversion works.
tempBinary = new StringBuilder(tempBinary).reverse().toString();
System.out.println("\nTherefore the letter " + word.charAt(i)
+ " is " + tempBinary
+ " When converted to binary based on ASCII tables.");
// Trailing space is irrelevant since it will be on the end of a println
binary += tempBinary + " ";
}
System.out.println("\n" + word
+ " translated to binary based on ASCII values is " + binary);
return binary;
}
/**
* This word will be converted into decimal, then binary and only the
* results will be printed
*
* @param word
*/
private static void NotVerbose(String word) {
// this method is static since this is a very small program and OOP is
// unnecessary for the very small scope
String binary = "";
System.out.print("The given input of "+word+" is ");
// this for loop will go over the entirety of the word and convert to
// binary
for (int i = 0; i < word.length()-1; i++) {
int letter = word.charAt(i);
binary += Integer.toBinaryString(letter) +" ";
}
System.out.print(binary);
// so the next output will be on the next line (probably not needed, but
// if this program were to be called by another program then it could be
// good)
System.out.print("\n");
}
/**
* This method will take in a string of binary, then convert it back to base ten. The binary MUST be separated by spaces.
* @param bin
*/
private static void BinaryToDecimal(String bin){
System.out.println("\nConverted back to ASCII:\n");
int binlen = bin.length(); //sets the length to a variable
boolean isSameNum= true; //used to track which number is being converted
String workShown = "";//used to hold a string of work to be shown
int sum = 0; //used to hold the sum of the numbers being added to get the binary
String binaryString = "";
int power = 0;//used to track 2 to the power of for binary to decimal conversion
for(int i = binlen-2; i>=-1; i--){//for loop counts down since it is easier to track what number to; add minus 2 to account for the extra space
if(i<0||bin.charAt(i)==' '){
isSameNum = false;
}
if(!isSameNum){
binaryString = new StringBuilder(binaryString).reverse().toString();
System.out.println(binaryString+" = "+workShown+" = "+sum+"\nTherefore based on the ASCII tables this number represents: "+ ((char)sum));
isSameNum = true;
binaryString = "";
workShown = "";
power = 0;
sum = 0;
}else{
binaryString+=bin.charAt(i);//used to show which binary string is getting converted
if(bin.charAt(i)=='1'){
sum+=Math.pow(2, (double)power);
workShown+=(int)Math.pow(2, (double)power);
if(i!=0&&bin.charAt(i-1)!=' '){//the i!=0 is evaluated first preventing an index out of bounds exception.
workShown+="+";
}
}
power+=1;
}
}
}
}