Search code examples
pythonfileraw-data

read \xHH escapes from file as raw binary in Python


I have following problem:

I want to read from file into a raw binary string :

The file looks like this (with escape characters, not binary data):

\xfc\xe8\x82\x00\x00\x00\x60\x89\xe5\x31\xc0\x64\x8b\x50\x30\x8b\x52

code used:

data = open("filename", "rb").read()

result obtained:

b"\\xfc\\xe8\\x82\\x00\\x00\\x00\\x60\\x89\\xe5\\x31\\xc0\\x64\\x8b\\x50\\x30\\x8b\\x52"

With dobule \ .

How can I read it as binary string like : \xaa characters ? (Without escape characters)


Solution

  • Ok. Your problem here is that you're asking the wrong question. Your data file isn't a raw binary string, it's an encoded one, encoded with escape characters. You're reading it as a raw binary, though, when you need instead to decode the escapes. Try

    data = open("filename", "r", encoding='unicode_escape').read().encode('raw_unicode_escape')
    

    instead.

    Edit: ok, this now works. You need to encode into raw_unicode_escape, not utf-8 (the default).