Python Forum

Full Version: hex file to binary or pcap to binary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have searched my questions a lot but I couldn't find any solutions, so here's my questions:

I have exported some txt files from pcap files using wireshark. I can also see this output using pkt.hexdump() from scapy module. I want to read the hex part and convert it into binary sequences.
As you can see in the image the file contains other characters such as ASCII that it's not needed. I know how to get an hex input from user or a file containing only hex and convert it, but I don't know how to deal with these files.
I also don't know if it's possible to convert pcap files to binary directly.

hexdec = input("Enter any number in Hexadecimal Format: ")

dec = int(hexdec, 16)
print(hexdec,"in Binary = ", bin(dec))
[Image: IKJGg3N.png]
you want to take a look at the libpcap library: https://pypi.org/project/libpcap/
github: https://github.com/karpierz/libpcap
docs: https://www.tcpdump.org/papers/bpf-usenix93.pdf

The pcap file is not just one record, but is a protocol of various records explained here: https://github.com/hokiespurs/velodyne-c...CAP-format
Thus to be able to read, you need to extract the various records

Quote:I want to read the hex part and convert it into binary sequences.
I know what you are asking here, but your terminology is incorrect as hex is just one representation of binary data, ASCII is another for printable character format, and does not have values for every binary value.

All binary can be displayed as hex, octal or decimal (and many formats, but not all binary can be displayed as ASCII, as ASCII is for printable characters only).

I think you will probably get what you are looking for with pyshark:
to install: pip install pyshark
github: https://github.com/KimiNewt/pyshark
documentation: Python wrapper for tshark, allowing python packet parsing using wireshark dissectors.