Objective: To input a .pcap file and output each byte (in binary form) on a new line.
Current code:
import java.io.FileInputStream;
public class display {
private static FileInputStream input;
public static void main(String[] args) {
try {
input = new FileInputStream("theDump.pcap");
int content = 0;
int counter = 0;
while ((content = input.read()) != -1) {
System.out.println(counter + ". " + (byte)content);
counter++;
}
} catch (Exception e) {
System.out.println("Error");
}
}
}
The .pcap file was made using tcpdump with the -w flag.
Current output of the code above:
0. -44
1. -61
2. -78
3. -95
4. 2
5. 0
6. 4
7. 0
8. 0
9. 0
... and so on...
I'm trying to get the output in readable binary, so 255 should be output as 1111 1111
Also, I'm not allowed to use any external library apart from the ones that come with JDK 8
You are looking for conversion of int
to binary, but haven't added anything in the code to do that. Integer
wrapper class contains a built-in API that may help you with that:
System.out.println(counter + ". " + Integer.toBinaryString(content));