-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCHAR_TO_BINARY.java
More file actions
41 lines (40 loc) · 1.17 KB
/
Copy pathCHAR_TO_BINARY.java
File metadata and controls
41 lines (40 loc) · 1.17 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
import java.io.*;
public class CHAR_TO_BINARY {
public static String AsciiToBinary(String asciiString){
byte[] bytes = asciiString.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
}
return binary.toString();
}
public static void main(String [] args) throws Exception {
File file = new File(args[0]);
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
StringBuilder stMaster = new StringBuilder();
while((st = br.readLine()) != null) {
stMaster.append(st);
stMaster.append("\n");
}
br.close();
BufferedWriter out = null;
try {
FileWriter fstream = new FileWriter(args[0]+".binary_encoding", true);
out = new BufferedWriter(fstream);
out.write(AsciiToBinary(stMaster.toString()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if(out != null) {
out.close();
}
}
}
}