简体   繁体   English

在Java中读取和输出.pcap文件

[英]Reading and output .pcap file in Java

Objective: To input a .pcap file and output each byte (in binary form) on a new line. 目标:输入.pcap文件并在新行上输出每个字节(二进制形式)。
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. .pcap文件是使用带有-w标志的tcpdump创建的。
Current output of the code above: 上面代码的当前输出:
0. -44 0。-44
1. -61 1. -61
2. -78 2. -78
3. -95 3. -95
4. 2 4. 2
5. 0 5. 0
6. 4 6. 4
7. 0 7. 0
8. 0 8. 0
9. 0 9. 0
... and so on... ... 等等...
I'm trying to get the output in readable binary, so 255 should be output as 1111 1111 我正在尝试以可读的二进制格式获取输出,因此应将255输出为1111 1111
Also, I'm not allowed to use any external library apart from the ones that come with JDK 8 另外,除JDK 8附带的库外,我不允许使用任何外部库

You are looking for conversion of int to binary, but haven't added anything in the code to do that. 您正在寻找将int转换为二进制的方法,但是还没有在代码中添加任何内容来实现这一点。 Integer wrapper class contains a built-in API that may help you with that: Integer包装器类包含一个内置的API ,可以帮助您:

System.out.println(counter + ". " + Integer.toBinaryString(content));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM