pythonbytedpkt

convert dpkt byte strings containing random characters


I am using the dpkt python module to parse a pcap file. I'm looking deep enough into the packets that some of the data is represented as byte streams. I can convert from regular byte strings easily enough, however some of the byte strings appear as:

\t\x01\x1c\x88

The first value should be 09, however for some reason it's using an escaped tab character. (the hex code of a tab is 09).

It's doing this for other characters in other streams as well.

Some more sample outputs:

\x10\x00@\x00\

\x05q\x00\x00\

\x069\x9c\n\x00

So my question is: can I convert this byte stream to one without these extra characters?

Alternatively, how would I go about converting something like '\t' to hex so that it returns '09'?

Update:

Turns out that I was creating the strings to be converted using a function that would return \t011c88 in place of the first stream.

Leaving it alone and using stream.encode("hex") worked


Solution

  • The repr function by default escapes all non-printable characters like you've seen.

    To get a hex-only representation, use

    string.encode("hex")
    

    NOTE: The original bytestream is correct, you should only convert to hex for viewing purposes rather than integity purposes. It only shows the data in a strange way.