在 Java 中读取和输出 .pcap 文件
Reading and output .pcap file in Java
Objective: 输入一个.pcap文件并在一个新行输出每个字节(以二进制形式)。
当前代码:
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");
}
}
}
.pcap 文件是使用带有 -w 标志的 tcpdump 创建的。
上面代码的当前输出:
0.-44
1.-61
2. -78
3. -95
4. 2
5. 0
6. 4
7. 0
8. 0
9. 0
...等等...
我正在尝试以可读二进制形式获取输出,因此 255 应输出为 1111 1111
此外,除了 JDK 8
附带的库外,我不允许使用任何外部库
您正在寻找将 int
转换为二进制文件的方法,但尚未在代码中添加任何内容来执行此操作。 Integer
包装器 class 包含一个 built-in API 可以帮助您:
System.out.println(counter + ". " + Integer.toBinaryString(content));
Objective: 输入一个.pcap文件并在一个新行输出每个字节(以二进制形式)。
当前代码:
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");
}
}
}
.pcap 文件是使用带有 -w 标志的 tcpdump 创建的。
上面代码的当前输出:
0.-44
1.-61
2. -78
3. -95
4. 2
5. 0
6. 4
7. 0
8. 0
9. 0
...等等...
我正在尝试以可读二进制形式获取输出,因此 255 应输出为 1111 1111
此外,除了 JDK 8
您正在寻找将 int
转换为二进制文件的方法,但尚未在代码中添加任何内容来执行此操作。 Integer
包装器 class 包含一个 built-in API 可以帮助您:
System.out.println(counter + ". " + Integer.toBinaryString(content));