Home > Enterprise >  how to convert python binary str literal to real bytes
how to convert python binary str literal to real bytes

Time:02-16

In python 3, I have a str like this, which is the exactly literal representation of bytes data:

'8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'

I would like to convert it to real byte,

b'8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'

I tried to use .encode() on the str data, but the result added many "xc2":

b'8\xc2\x81p\xc2\x925\x00\x003dx\xc2\x91P\x00x\xc2\x923\x00\x00\xc2\x91Pd\x00\xc2\x921d\xc2\x81p1\x00\x00'.

I also tried:

import ast

ast.literal_eval("b'8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'")

The result is:

ValueError: source code string cannot contain null bytes

How to convert the str input to the bytes as exactly the same as follows?

b'8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'

CodePudding user response:

You are on the right track with the encode function already. Just try with this encoding:

>>> '8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'.encode('raw_unicode_escape')
b'8\x81p\x925\x00\x003dx\x91P\x00x\x923\x00\x00\x91Pd\x00\x921d\x81p1\x00\x00'

I took it from this table in Python's codecs documentation

Edit: I just found it needs raw_unicode_escape instead of unicode_escape

  • Related