Python Forum
hex file to binary or pcap to binary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: hex file to binary or pcap to binary (/thread-23103.html)



hex file to binary or pcap to binary - baran01 - Dec-11-2019

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]


RE: hex file to binary or pcap to binary - Larz60+ - Dec-11-2019

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-copter/wiki/PCAP-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.